如何使用 FolderBrowserDialog 保存和加载用户最后选择的文件夹?
How can I save and load the last selected folder by user with FolderBrowserDialog?
private void btnStart_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastSelectedFolder = openFolderDialog.SelectedPath.ToString();
Properties.Settings.Default.Save();
}
LastSelectedFolder 不存在。
我尝试转到项目属性的“设置”选项卡,然后我将 LastSelectedFolder 添加到值中。
所以现在我有:名称设置类型字符串范围用户值 LastSelectedFolder
但它仍然无法正常工作,仍然出现错误,并且在保存到哪里以及如何在单击 btnStart 时将其加载回来?
LastSelectedFolder 在默认后不存在,即使将其添加到设置中也是如此:
试试这个:
private void btnStart_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
openFolderDialog.SelectedPath = Properties.Settings.Default.LastSelectedFolder;
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastSelectedFolder = openFolderDialog.SelectedPath.ToString();
Properties.Settings.Default.Save();
}
如果您创建一个新对话框,您必须将最后一个路径设置为默认路径。因此您也可以使用 FolderBrowserDialog.SelectedPath
属性。这是带有附加行的代码:
private void btnStart_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
openFolderDialog.SelectedPath = Properties.Settings.Default.LastSelectedFolder;
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastSelectedFolder = openFolderDialog.SelectedPath.ToString();
Properties.Settings.Default.Save();
}
}
在您的屏幕截图中,您将设置命名为 Setting
。将此更改为 LastSelectedFolder
并清除默认值(最后一列)。
之后就可以编译了运行!
private void btnStart_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastSelectedFolder = openFolderDialog.SelectedPath.ToString();
Properties.Settings.Default.Save();
}
LastSelectedFolder 不存在。 我尝试转到项目属性的“设置”选项卡,然后我将 LastSelectedFolder 添加到值中。
所以现在我有:名称设置类型字符串范围用户值 LastSelectedFolder
但它仍然无法正常工作,仍然出现错误,并且在保存到哪里以及如何在单击 btnStart 时将其加载回来?
LastSelectedFolder 在默认后不存在,即使将其添加到设置中也是如此:
试试这个:
private void btnStart_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
openFolderDialog.SelectedPath = Properties.Settings.Default.LastSelectedFolder;
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastSelectedFolder = openFolderDialog.SelectedPath.ToString();
Properties.Settings.Default.Save();
}
如果您创建一个新对话框,您必须将最后一个路径设置为默认路径。因此您也可以使用 FolderBrowserDialog.SelectedPath
属性。这是带有附加行的代码:
private void btnStart_Click(object sender, EventArgs e)
{
System.Windows.Forms.FolderBrowserDialog openFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
openFolderDialog.SelectedPath = Properties.Settings.Default.LastSelectedFolder;
if (openFolderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.LastSelectedFolder = openFolderDialog.SelectedPath.ToString();
Properties.Settings.Default.Save();
}
}
在您的屏幕截图中,您将设置命名为 Setting
。将此更改为 LastSelectedFolder
并清除默认值(最后一列)。
之后就可以编译了运行!