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

问题描述 投票:2回答:4

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

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

我想保留最后选择的路径。这是代码:private void testFileButton_Click(object sender,EventArgs e){fd = new OpenFileDialog(); fd.FileName = testParameters ....

c# .net openfiledialog
4个回答
0
投票

documentation状态:


0
投票
  1. 您没有在各处使用相同的变量进行文件对话框。就像在if块中一样,您正在显示文件对话框fd,但在其他部分中,您正在使用变量openFileDialog。我不明白您为什么要这么做,或者可能是错字。如果要将取消对话框的初始目录设置为"C:\",请在两个地方使用相同的变量。]​​>


0
投票

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


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");
            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.