使用OpenFileDialog / C#/ .Net保留最后选择的路径

时间:2014-07-30 08:14:11

标签: c# .net openfiledialog

我想保留所选的最后一条路径。这是代码:

private void testFileButton_Click(object sender, EventArgs e)
{
     fd = new OpenFileDialog();
     fd.FileName = testParameters.testFileFile;
     fd.InitialDirectory = testParameters.testFileDir;

     if (fd.ShowDialog() == DialogResult.OK)
     {                    
         try
         {
            if (fd.SafeFileName != null)
             {
                 testParameters.testFileDir = Path.GetDirectoryName(fd.FileName);
                 testParameters.testFileFile = Path.GetFileName(fd.FileName);
                 testFileLabel.Text = fd.FileName;                 
             }           
          }
          catch (IOException)
          {
              MessageBox.Show("Error: Could not read file");
          }
      }
} 

为了能够保留最后选择的路径,我尝试添加RestorDirectory和索引,但我没有得到任何结果:

private void testFileButton_Click(object sender, EventArgs e)
{
     fd = new OpenFileDialog();
     fd.FileName = testParameters.testFileFile;
     fd.InitialDirectory = testParameters.testFileDir;
     fd.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
     fd.FilterIndex = 2;
     fd.RestoreDirectory = true;

     if(...){
     ...
     }
}

然后我尝试使用“else”代替:

private void testFileButton_Click(object sender, EventArgs e)
{
    fd = new OpenFileDialog();
    fd.FileName = testParameters.testFileFile;
    fd.InitialDirectory = testParameters.testFileDir;

     if (fd.ShowDialog() == DialogResult.OK)
     {                    
         try
         {
             if (fd.SafeFileName != null)
             {
                 testParameters.testFileDir = Path.GetDirectoryName(fd.FileName);
                 testParameters.testFileFile = Path.GetFileName(fd.FileName);
                 testFileLabel.Text = fd.FileName;                 
             }  
         } 


     }
     catch (IOException)
     {
         MessageBox.Show("Error: Could not read file");
     }


    }
    else
    {
         openFileDialog1.InitialDirectory = @"C:\";
    }
} 

但又没有任何结果..

有没有人有任何想法?

编辑:上次尝试

private void testFileButton_Click(object sender, EventArgs e)
        {
             //http://stackoverflow.com/questions/7793158/obtaining-only-the-filename-when-using-openfiledialog-property-filename
             OpenFileDialog fd = new OpenFileDialog();
             fd.FileName = testParameters.testFileFile;
             Environment.CurrentDirectory = @"C:\" ;

             if (fd.ShowDialog() == DialogResult.OK )
             {                    
                 try
                 {
                    if (fd.SafeFileName != null)
                     {
                         string ffileName = fd.FileName;
                         testParameters.testFileDir = Path.GetDirectoryName(ffileName);
                         testParameters.testFileFile = Path.GetFileName(ffileName);
                         testFileLabel.Text = ffileName;

                     }

                  }
                  catch (IOException)
                  {
                      MessageBox.Show("Error: Could not read file");
                  }
              }
        }   

5 个答案:

答案 0 :(得分:0)

documentation州:

  

如果对话框将当前目录恢复为原始值,则为true   用户在搜索文件时更改了目录;否则,错误。

更新:稍微改变我的回答,因为我认为我可能误解了你的意图:

如果我没有弄错的话,每次打开对话框时,对话框都会打开fd.InitialDirectory,因为根据定义,这是fd新实例的默认设置。我相信这可能是你的问题:每次尝试打开它时都会调用fd = new OpenFileDialog();

如果你改变你的代码每次都使用fd的相同实例(在外部范围内定义它,例如通过Property访问单例实例?),它可能会记住它以前的目录本身 - 那个是默认行为(您可以使用RestoreDirectory属性覆盖)。

获取单例实例的示例:这只会实例化对话框一次,并在每次调用属性时返回相同的实例:

Private OpenFileDialog _fd;
private OpenFileDialog SingleFd {
    get { return _fd ?? (_fd = new OpenFileDialog()); }
}


// Now in your method, use:
var singleInstance = SingleFd;

答案 1 :(得分:0)

  1. 您没有在任何地方使用相同的文件对话框变量。就像在if块中显示文件对话框fd一样,但在其他部分你使用变量openFileDialog。我无法理解你为什么这样做,或者可能是一个错字。如果要在用户取消对话框时将初始目录设置为"C:\",请在两处使用相同的变量。

  2. 不是在每次调用中创建实例,而是创建一次实例并将其用于后续调用。

  3. 关于目录恢复,如果我们不设置初始目录,默认情况下它会恢复以前的目录,即使对于不同的实例也是如此。

答案 2 :(得分:0)

OpenFileDialog最初使用当前目录,这是一个进程范围的设置。您已通过设置InitialDirectory来覆盖此行为。只是不要这样做,它会起作用。

如果要保留跨进程重新启动时使用的最后一个目录,请捕获Environment.CurrentDirectory并保存。在打开对话框之前设置它。

请注意,当前目录是一个进程范围的设置,因此应用程序的不同部分可能会产生干扰。此外,所有相对路径都是相对于此目录解释的(这就是为什么相对路径通常是等待的bug)。

答案 3 :(得分:0)

string path = @"C:\";
        OpenFileDialog fd = new OpenFileDialog();
        fd.FileName = "SelectFolder";
        fd.InitialDirectory =path;
        fd.ValidateNames = false;
        fd.CheckFileExists = false;

        if (fd.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if (fd.SafeFileName != null)
                {
                    string txt1 = System.IO.Path.GetFullPath(fd.FileName),
                        txt2 = txt1.Replace("SelectFolder", "").Trim();
                    testFileLabel.Text = txt2.Replace(path, "").Replace(@"\", ""); ;
                }
            }
            catch (IOException)
            {
                MessageBox.Show("Error: Could not read file");
            }
        }

答案 4 :(得分:-1)

解决方案是将InitialDirectory路径设置为空白

openFileDialog.InitialDirectory = ""

openFileDialog.RestoreDirectory = True
相关问题