C#winform 设置打开文件夹选择对话框folderBrowserDialog自动定位至上一次的选择路径(新手记录)

第一步:布局

控件:一个textBox1,一个button1,一个folderBrowserDialog1
在这里插入图片描述

第二步:创建一个公共类

用来存储folderBrowserDialog1上一次的选择值

public static string PassFolderDialogSelectPath { get; set; }

第三步:代码

button1点击代码:选择一个文件夹,并将选择路径赋值给textBox1

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "选择文件夹"; 
            //MyFormPassValue.PassFolderDialogSelectPath是我的公共类
            if (MyFormPassValue.PassFolderDialogSelectPath != null)
            {
                folderBrowserDialog1.SelectedPath = MyFormPassValue.PassFolderDialogSelectPath;
            }
            textBox1.Text = folderBrowserDialog1.SelectedPath;
        }

窗体关闭代码:关闭窗体时,将textBox1的文本赋值给公共类

        private void folderBrowserDialogTestForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MyFormPassValue.PassFolderDialogSelectPath = textBox1.Text;
        }

到此为止,实现暂时的路径保存,但是,软件打开再关闭就失效。

第四步:写入配置文件App.config或者txt文件

App.config文件代码:注意添加引用System.Configuration
在这里插入图片描述
窗体关闭代码:添加写入App.config代码

        private void folderBrowserDialogTestForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MyFormPassValue.PassFolderDialogSelectPath = textBox1.Text;
            //窗体关闭时,获取文件夹对话框的路径写入配置文件中
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["FolderSelectedPath"].Value = MyFormPassValue.PassFolderDialogSelectPath;
            //一定要记得保存,写不带参数的config.Save()也可以
            config.Save(ConfigurationSaveMode.Modified);
            //刷新,否则程序读取的还是之前的值(可能已装入内存)
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
            //创建并写入txt文件
            //string str;
            //str = MyFormPassValue.PassFolderDialogSelectPath;
            //StreamWriter sw = new StreamWriter(Application.StartupPath + "\\FolderLastPath.txt",false);//false表示覆盖,true表示追加
            //sw.Write(str);
            //sw.Close();//写入          
        }

第五步:读取配置文件App.config 或者txt文件

窗体加载代码:

        private void folderBrowserDialogTestForm_Load(object sender, EventArgs e)
        {
            //窗体加载时,获取写入配置文件中的路径
            MyFormPassValue.PassFolderDialogSelectPath = ConfigurationManager.AppSettings["FolderSelectedPath"];
             //读取txt文件
            //if (File.Exists(Application.StartupPath + "\\FolderLastPath.txt"))
            //{
            //    string str;
            //    StreamReader sr = new StreamReader(Application.StartupPath + "\\FolderLastPath.txt", false);
            //    str = sr.ReadLine().ToString();
            //    sr.Close();
            //    MyFormPassValue.PassFolderDialogSelectPath = str;///读取
            //}
        }

到此为止,就可以实现关闭软件再打开时,也可以实现打开文件夹选择对话框时定位至上一次的选择路径。

第六步:问题解答,特别说明

你会发现,路径根本写不进去配置文件中,没有任何效果。
原因是………………
解决办法:
将你debug或者release后生成的文件夹,即bin\下的debug或者release文件夹直接拷贝出来,放到另外的路径下,再打开里面生成的.exe文件试一试。
PS:今天将文件打包时发现,关闭软件时会报错,对配置文件的访问被拒绝。后来发现这是因为win10的C盘权限需要管理员权限,可以在下面的文件里更改权限:(划线处修改成管理员权限)
在这里插入图片描述
在这里插入图片描述

扫描二维码关注公众号,回复: 4659736 查看本文章

猜你喜欢

转载自blog.csdn.net/D_lunar/article/details/84991615