FolderBrowserDialog实现默认选中上次选择的目录

当需频繁选择目录时,如果不做任何设置,FolderBrowserDialog默认定位到“桌面”。通过设置dialog.RootFolder的属性,可以指定系统常见目录为根目录,具体请查看Environment.SpecialFolder属性。

但Environment.SpecialFolder属性无法提供用户“普通”目录的定位。其解决方法如下:

public partial class Form1 : Form { string defaultfilePath = ""; private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog dialog = new FolderBrowserDialog(); //首次defaultfilePath为空,按FolderBrowserDialog默认设置(即桌面)选择 if (defaultfilePath != "") { //设置此次默认目录为上一次选中目录 dialog.SelectedPath = defaultfilePath; } if (dialog.ShowDialog() == DialogResult.OK) { //记录选中的目录 defaultfilePath = dialog.SelectedPath; } } }

你可能感兴趣的:(Web后端(ASP.NET))