2012-04-02 369 views
15
using (var openFileDialog1 = new OpenFileDialog()) 
     { 
      openFileDialog1.Reset(); 
      if (!string.IsNullOrEmpty(ExcelFilePath)) 
      { 
       string fileName = Path.GetFileName(ExcelFilePath); 
       string fileExt = Path.GetExtension(ExcelFilePath); 
       //Avoid "you can't open this location using this program file" dialog 
       //if there is a file name in the path strip it) 
       if (!string.IsNullOrEmpty(fileName)) 
        initialDirectory = Path.GetDirectoryName(ExcelFilePath); 
     //if not let it be 
       else 
        initialDirectory = ExcelFilePath; 

      openFileDialog1.InitialDirectory = initialDirectory; 
      } 
      else 
       openFileDialog1.InitialDirectory = "c:\\"; 
      openFileDialog1.Filter = "Excel files (*.xls or *.xlsx)|*.xls;*.xlsx"; 
      //openFileDialog1.Filter = "xls files (*.xls)|*.xls|xlsx files(*.xlsx)|.xlsx"; 
      openFileDialog1.FilterIndex = 2; 
      openFileDialog1.RestoreDirectory = false; 
      openFileDialog1.CheckFileExists = true; 
      openFileDialog1.CheckPathExists = true; 
      if (openFileDialog1.ShowDialog() == DialogResult.OK) 
      { 
       var browseSelectionMade = BrowseSelectionMade; 
       if (browseSelectionMade!=null) 
        browseSelectionMade(this, new DataEventArgs<string>(openFileDialog1.FileName)); 
      } 
     } 

无论我是否将RestoreDirectory设置为true,如果我的初始目录设置为不存在的路径,我将始终浏览到LAST使用的目录。 OpenFileDialog保存的最后一个使用目录在哪里?有没有办法来覆盖这种行为? (例如,如果初始目录不存在,我总是希望将它设置为C:\)OpenFileDialog默认路径

回答

11

哪里是最后使用的目录保存?

它存储在注册表中。确切位置取决于Windows版本,对于Win7,它是HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ ComDlg32。用reregit快速查看应该说服你,你不要想惹的祸。

简单的解决方法是提供一个有效的路径。如果您计算的那个无效,Directory.Exists返回false,然后提供一个有效的。像Environment.GetFolderPath()返回的Documents文件夹一样。然后,再次,上次使用的一个也没什么问题,用户会很容易地识别出它碰巧接近所需的那个。

+0

感谢您的答复,我会将您的答案标记为已接受的答案,尽管第二个答案也很好。事实上,我最终确定了路径的存储位置,这对我来说是很有帮助的。再次感谢! – 2012-04-02 17:26:57

+0

Upvoting _Hans Passant_感觉如此毫无意义...... – itsho 2017-01-08 13:28:12

1

检查ExcelFilePath是否存在,检查它是否为空或空,但是如果在您阻止之前检查目录是否存在,如果它没有将值重置为空字符串,则应该是黄金。

(是的,你需要等先申请文件名逻辑),但是一旦你解析的说了出来,这是微不足道的,以确定该目录退出

if (!Directory.Exists(excelPath)) 
{ 
    ExcelFilePath = String.Empty; 
} 
24

好像你需要做的是以下几点:

string path; // this is the path that you are checking. 
if(Directory.Exists(path)) { 
    openFileDialog1.InitialDirectory = path; 
} else { 
    openFileDialog1.InitialDirectory = @"C:\"; 
} 

这是除非我失去了一些东西。

+0

openFileDialog1.InitialDirectory = Directory.Exists(path)?路径:@“C:\”; – 2017-08-28 04:09:58

3

我不认为有什么内置的。只是检查你打开对话框之前:

if (!Directory.Exists(initialDirectory)) 
{ 
    openFileDialog1.InitialDirectory = @"C:\"; 
} 
0

如果你使用存储在某些字符串的文件名,这是更好地使用路径切的文件名(在我的W10的打开的对话框中不初始目录中打开,如果我只是供应文件名):

if (!System.IO.Directory.Exists(filename)) 
    { 
     openDlg.InitialDirectory = 
      System.IO.Path.GetDirectoryName(filename); 
    } 
0

对于未来我

记得做:

  try   
      { 
       result = dialog.ShowDialog(Window); 
      } 
      catch 
      { 
       dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
       result = dialog.ShowDialog(Window); 
      } 

这有助于在用户从位置打开文件的情况下,该文件不再存在(例如, USB棒,映射网络驱动器) - 如果InitialDirectory无效,ShowDialog会抛出异常。