简体   繁体   English

如何记住 FolderBrowserDialog 中最后选择的文件夹?

[英]How to remember the last selected folder in FolderBrowserDialog?

I am using FolderBrowserDialog in my application.Through it's constructor I can set RootPath , SelectedPath .我在我的应用程序中使用FolderBrowserDialog 。通过它的构造函数,我可以设置RootPathSelectedPath It should always open with D:\Export\ directory as default path.它应该始终以D:\Export\ 目录作为默认路径打开。 If the user selects any other path, the newly selected directory should be reflected on folder.SelectedPath variable also if the user closes the dialog window and open it again, it should open with last time selcted folder(User selected folder).如果用户选择任何其他路径,新选择的目录应反映在文件夹中。SelectedPath变量也如果用户关闭对话框 window 并再次打开它,它应该打开上次选择的文件夹(用户选择的文件夹)。 It should not open the default folder (D:\Export).它不应打开默认文件夹 (D:\Export)。

public void OpenFolderDialog()
{
FolderBrowserDialog folder = new FolderBrowserDialog(Environment.SpecialFolder.MyComputer, @"D:\Export");
folder.ShowDialog();

    if(!string.IsNullOrEmpty(folderBrowserDialog.SelectedPath) && Directory.Exists(folderBrowserDialog.SelectedPath))
    {
        ExportData(folderBrowserDialog.SelectedPath);
    }
    else
    {
        if (string.IsNullOrEmpty(folderBrowserDialog.SelectedPath))
        {
          log.WarningMsg("FolderBrowserDialog selected path is empty");
         }
         else
         {
           log.WarningMsg("FolderBrowserDialog selected Directory Does not exist");
         }
      }
 }

Note: folderBrowserDialog.SelectedPath is readonly property.注意:folderBrowserDialog.SelectedPath 是只读属性。 we cant able to assign any values in it.我们无法在其中分配任何值。

How can we remember the Last Selected Folder path?我们如何记住最后选择的文件夹路径?

You could store the value of the SelectedPath property in a variable as soon as the ShowDialog() method returns:您可以在ShowDialog()方法返回后立即将SelectedPath属性的值存储在变量中:

private string _selectedPath = @"D:\Export\"; // <-- the default value
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
    FolderBrowserDialog dialog = new FolderBrowserDialog()
    {
        SelectedPath = _selectedPath
    };
    dialog.ShowDialog();
    _selectedPath = dialog.SelectedPath;
}

If you want to use System.Windows.Forms.FolderBrowserDialog , you're going to have to store the SelectedPath value in a field or property, then set SelectedPath back to that value on subsequent calls before calling ShowDialog again (as shown in mm8's answer ).如果要使用System.Windows.Forms.FolderBrowserDialog ,则必须将SelectedPath值存储在字段或属性中,然后在再次调用ShowDialog之前将SelectedPath设置回该值(如mm8 的答案所示)。

But honestly, I wouldn't bother with that.但老实说,我不会为此烦恼。 To me, the real answer is not to use FolderBrowserDialog at all .对我来说,真正的答案是根本不使用FolderBrowserDialog That dialog is very unfriendly from the user's point of view.从用户的角度来看,该对话框非常不友好。 As recommend in this answer to the question How to use OpenFileDialog to select a folder?正如对问题如何使用 OpenFileDialog 到 select 文件夹回答中所建议的那样? , I too recommend using Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog . ,我也推荐使用Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog It's part of a Microsoft NuGet package that can be found here: https://www.nuget.org/packages/Microsoft.WindowsAPICodePack-Shell . It's part of a Microsoft NuGet package that can be found here: https://www.nuget.org/packages/Microsoft.WindowsAPICodePack-Shell .

This dialog automatically does what you are trying to do: it remembers the last folder the user selected and starts in that folder the next time to user opens the dialog- no additional code required.此对话框会自动执行您尝试执行的操作:它会记住用户选择的最后一个文件夹,并在下次用户打开对话框时从该文件夹开始 - 不需要额外的代码。

Example usage:示例用法:

var dialog = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog { IsFolderPicker = true };
if (dialog.ShowDialog() == Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogResult.Ok)
{
    string selected = dialog.FileName;
}

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

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