简体   繁体   English

如何使用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();
}

The LastSelectedFolder not exist. LastSelectedFolder不存在。 I tried to go to the project properties to the Settings tab and there i added to the value the LastSelectedFolder. 我尝试转到项目属性的“设置”选项卡,然后在其中添加了LastSelectedFolder值。

So now I have: Name Setting Type string Scope user Value LastSelectedFolder 所以现在我有了:名称设置类型字符串范围用户值LastSelectedFolder

But it's not working still getting the error and also after saving where and how do I load it back when clicking the btnStart? 但是它仍然无法正常工作,并且仍然出现错误,并且在保存位置以及单击btnStart时如何将其重新加载?

The LastSelectedFolder is not exist after the Default even after added it to the Settings: 即使将默认值添加到“设置”中,LastSelectedFolder也不会存在:

设定值

You have to set the last path as default if you create a new dialog. 如果创建新对话框,则必须将最后一个路径设置为默认路径。 Therefore you can use also the FolderBrowserDialog.SelectedPath property. 因此,您也可以使用FolderBrowserDialog.SelectedPath属性。 Here is your code with the additional line: 这是带有附加行的代码:

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

In your screenshot you named your setting Setting . 在屏幕快照中,您将Setting命名为Setting Change this one to LastSelectedFolder and clear the default value (last column). 将此更改为LastSelectedFolder并清除默认值(最后一列)。

After that you can compile and run! 之后,您可以编译并运行!

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM