首页 > Windows开发 > 详细

1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)

时间:2017-01-03 16:37:17      阅读:840      评论:0      收藏:0      [点我收藏+]

C#的OpenFileDialog的常用属性设置

1、设置属性

1)设置弹出的指定路径(绝对路径、相等路径)

2)设置标题

3)设置文本格式

 

2、打开方式1(绝对路径)

2.1) 打开的路径

技术分享

2.2) 方式1源码

 /// <summary>
        /// 指定绝对路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //创建对象
            OpenFileDialog ofg = new OpenFileDialog();
            //判断保存的路径是否存在
            if (!Directory.Exists(@"D:\Test\Debug1"))
            {
                //创建路径
                Directory.CreateDirectory(@"D:\Test\Debug1");
            }
            //设置默认打开路径(绝对路径)
            ofg.InitialDirectory =  @"D:\Test\Debug1";
            //设置打开标题、后缀
            ofg.Title = "请选择导入xml文件";
            ofg.Filter = "xml文件|*.xml";
            string path = "";
            if (ofg.ShowDialog() == DialogResult.OK)
            {
                //得到打开的文件路径(包括文件名)
                path = ofg.FileName.ToString();
                MessageBox.Show("打开文件路径是:" + path);
            }
            else if (ofg.ShowDialog() == DialogResult.Cancel)
            {
                MessageBox.Show("未选择打开文件!");
            }
        }

 2.3)方式1的运行结果

技术分享

3、打开方式2(相对路径)

3.1) 打开路径2

技术分享

3.2)方式2源码

         /// <summary>
        /// 指定相对路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //创建对象
            OpenFileDialog ofg = new OpenFileDialog();
            //判断保存的路径是否存在
            if (!Directory.Exists(Application.StartupPath + @"\Test\Debug1"))
            {
                //创建路径
                Directory.CreateDirectory(Application.StartupPath + @"\Test\Debug1");
            }
            //设置默认打开路径(项目安装路径+Test\Debug1\)
            ofg.InitialDirectory = Application.StartupPath + @"\Test\Debug1";
            //设置打开标题、后缀
            ofg.Title = "请选择导入xml文件";
            ofg.Filter = "xml文件|*.xml";
            string path = "";
            if (ofg.ShowDialog() == DialogResult.OK)
            {
                //得到打开的文件路径(包括文件名)
                path = ofg.FileName.ToString();
                MessageBox.Show("打开文件路径是:" + path);
            }
            else if (ofg.ShowDialog() == DialogResult.Cancel)
            {
                MessageBox.Show("未选择打开文件!");
            }
        }    

 3.3)方式2结果视图

技术分享

1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)

原文:http://www.cnblogs.com/xielong/p/6245218.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!