如何用户使用FolderBrowserDialog保存并加载最后选择的文件夹?

时间:2017-02-09 16:44:59

标签: c# .net winforms 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添加到“设置”后,LastSelectedFolder也不存在:

Settings

2 个答案:

答案 0 :(得分:1)

如果创建新对话框,则必须将最后一个路径设置为默认路径。因此,您还可以使用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并清除默认值(最后一列)。

之后你可以编译并运行!

答案 1 :(得分:0)

试试这个:

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();
    }