2009-07-24 238 views
29

我想一个SaveFileDialog有以下行为:设置SaveFileDialog的初始目录?

  • 当你第一次打开它,它关系到“我的文档”。

  • 之后,它会转到最后选择的文件夹。 完成此操作的最佳方法是什么?

如果我没有设置InitialDirectory,它会进入exe的目录 - 这不是我想要的。它记住了最后选择的目录 - 即使在执行之间。

如果我设置InitialDirectory,它不记得最后选择的目录。当然,我可以保存在注册表中最后选定的目录:(但我正在寻找一个更好的解决方案。

 SaveFileDialog dialog = new SaveFileDialog(); 
     //??? dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
     dialog.ShowDialog(); 

有什么建议?

回答

26

您需要设置RestoreDirectorytrue还有InitialDirectory财产

+3

啊,那是什么RestoreDirectory是。 ... – 2009-07-24 00:38:45

+3

另请参见此线程,其中程序员说将“RestoreDirectory”设置为true并不会帮助: http://discuss.joelonsoftware.com/default.asp?dotnet.12.424113.2 – SLA80 2009-12-30 14:43:40

+6

真的吗?不适合我。它总是导航到InitialDirectory? – 2010-09-20 00:31:35

0

我做了一些与.NET 2.0的测试,似乎如果我将FileName设置为除文字字符串以外的任何东西,它都不起作用当我使用方法或accesstor设置属性时,初始目录是忽略。

13

我不知道为什么这会起作用,但我终于能够为我工作。

我发现,如果我给完整路径,它不会工作,但如果我把Path.GetFullPath()内的完整路径,那么它会工作。看着前后的价值观表明它们是相同的,但如果没有它,它将始终不起作用,并与之合作。

//does not work 
OpenFileDialog dlgOpen = new OpenFileDialog(); 
string initPath = Path.GetTempPath() + @"\FQUL"; 
dlgOpen.InitialDirectory = initPath; 
dlgOpen.RestoreDirectory = true; 

//works 
OpenFileDialog dlgOpen = new OpenFileDialog(); 
string initPath = Path.GetTempPath() + @"\FQUL"; 
dlgOpen.InitialDirectory = Path.GetFullPath(initPath); 
dlgOpen.RestoreDirectory = true; 
+3

这对我没有帮助(Windows 7,64位,.net框架4.5.1) – danio 2016-01-28 17:10:05

+0

@danio我有Windows 7 64位,这为我工作。我的应用程序使用.NET 4. – 2017-07-07 06:58:24

3

我也尝试了多种不同的“解决方案”在不同的地方发现的,但他们都不是存在于注册表中的MRU列表条目尽快工作:/但这里是我自己简单的解决方法......

而不是设置对话框的InitialDirectory属性,设置FileName属性到您的路径,但所选择的Filter,如结合:

dialog.FileName = Path.Combine(myPath, "*.*"); 
4

请一定要检查该目录路径存在在设置初始目录属性之前。如果该目录不存在,则创建该目录。即

if (!Directory.Exists(FooDirectory)) 
{ 
    Directory.CreateDirectory(FooDirectory); 
} 
0

如果在路径的任何位置使用正斜杠,InitialDirectory不起作用。确保将它们转换为反斜杠

0
savefiledialog.InitialDirectory = Application.StartupPath; 
savefiledialog.RestoreDirectory = true; 

第二次测试之前。

1

所提供的解决方案都不适合我。

除OP规范外,我希望程序记住运行之间的最后保存位置。对于这一点,在视觉工作室解决方案资源管理器下ProjectName -> Properties -> Settings.settings,我设置以下属性:

Settings.settings property: Name=PreviousPath, Type=string, Scope=User, leave Value empty

因为我保持SaveFileDialog周围程序的运行期间,我在实例化开始。然后在Command为抚育对话框:

if (string.IsNullOrEmpty(Settings.Default.PreviousPath)) 
{ 
    this.saveDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); 
} 
else 
{ 
    this.saveDialog.InitialDirectory = Settings.Default.PreviousPath; 
} 

this.saveDialog.FileName = "Default_File_Name"; 

bool result = this.saveDialog.ShowDialog() ?? false; 

if (result) 
{ 
    Settings.Default.PreviousPath = Path.GetDirectoryName(this.saveDialog.FileName); 
    Settings.Default.Save(); 

    // Code writing to the new file... 
} 

这使行为:

  • 程序运行,并且打开的对话框第一次,它定位到“我的文档”。
  • 连续运行,并打开对话框:
    • 如果保存在一个打开的,它定位到以前的保存位置。
    • 如果未保存在先前的打开中,则导航到“我的文档”。
1

的建议解决方案并没有为我工作,所以寻找How does WPF OpenFileDialog track directory of last opened file?后,我实现:

public static void SetInitialDirectory(this FileDialog dlg, string fileExtension, string initialDirectory) 
     { 
      // RestoreDirectory doesn't seem to be implemented - https://stackoverflow.com/questions/11144770/how-does-wpf-openfiledialog-track-directory-of-last-opened-file 
      // so manually only set InitialDirectory if nothing is stored 
      try 
      { 
       var mru = @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\" + fileExtension; 
       var rk = Registry.CurrentUser.OpenSubKey(mru); 
       if (rk == null) 
       { 
        dlg.InitialDirectory = initialDirectory; 
       } 
      } 
      catch (Exception) 
      { 
       // SecurityException, ObjectDisposedException => allow default behaviour 
      } 
     } 

这将使用提供initialDirectory如果对话框没有被使用过该文件延期。一旦使用了对话框,它就会恢复到记住前一个目录的默认行为。

1

我发现设置InitialDirectorynull首先围绕用户历史记录工作。

OpenFileDialog dlgOpen = new OpenFileDialog(); 
    dlgOpen.InitialDirectory = null; 
    dlgOpen.InitialDirectory = @"c:\user\MyPath"; 
0

这是我结束了,与所述OP想指出的对话框随之而来:

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
dlg.InitialDirectory = null; 

// May or may not want "saves", but this shows how to append subdirectories 
string path = (Path.Combine(Environment.ExpandEnvironmentVariables("%USERPROFILE%"), "My Documents") + "\\saves\\"); 

if (!Directory.Exists(path)) 
    Directory.CreateDirectory(path); 

dlg.InitialDirectory = path; 
dlg.RestoreDirectory = true; 

if (dlg.ShowDialog().Value == true) 
{ 
    // This is so you can have JUST the directory they selected 
    path = dlg.FileName.Remove(dlg.FileName.LastIndexOf('\\'), dlg.FileName.Length - dlg.FileName.LastIndexOf('\\')); 

    string filePath = Path.Combine(path, fileName); 

    // This is "just-in-case" - say they created a new directory in the dialog, 
    // but the dialog doesn't seem to think it exists because it didn't see it on-launch? 
    if (!Directory.Exists(path)) 
     Directory.CreateDirectory(path); 

    // Remove a file if it has the same name 
    if (File.Exist(filePath)) 
     File.Delete(filePath); 

    // Write the file, assuming you have and pass in the bytes 
    using (FileStream fs = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write) 
    { 
     fs.Write(bytes, 0, (int)bytes.Length); 
     fs.Close(); 
    } 
} 
0

只想在权衡这个讨论(一两年为时已晚),因为我也有确切的问题。 即使人们认为这种行为 - 即第一次使用默认路径,然后使用之前选定的路径 - 可以通过使用openFileDialog的属性和函数来实现,但不能(现在)!

人们可以改变工作目录所需的默认路径 (例如Environment.CurrentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);) 但一些框架,像团结,不喜欢这种非常多,终止。

正因为如此我结束了这段代码:

private bool firstDialogOpened = true; 

public void BrowseFiles() 
{ 
    OpenFileDialog openFileDialog = new OpenFileDialog(); 
    openFileDialog.Filter = "Model files (*.obj)|*.obj|All files (*.*)|*.*"; 
    openFileDialog.FilterIndex = 1; 

    if (firstDialogOpened) 
    { 
     openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal); 
     firstDialogOpened = false; 
    } 

    if (openFileDialog.ShowDialog() == DialogResult.OK) 
     filepath.text = openFileDialog.FileName; 
} 

这为我所期望的行为,但我会喜欢一个更优雅的解决方案。

0

“在保存的输出文件,以在vb.net中所需的目录来看, ”下面是我发现,工作就像一个魅力的方式:

Dim filcsv As String = fileNamey.Replace(".txt", "_Denied2.csv") 
Dim filcsv2 As String = fileNamey.Replace(".txt", "_Approved2.csv") 

Dim outputDirectory As String = "C:\Users\jlcmil\Documents\EnableMN\output\" 
Dim totalPath As String = System.IO.Path.Combine(outputDirectory, filcsv) 
Dim totalPath2 As String = System.IO.Path.Combine(outputDirectory, filcsv2) 

Dim outDenied As StreamWriter = New StreamWriter(totalPath) 
Dim outApproved As StreamWriter = New StreamWriter(totalPath2)