我想保留选定的最后一条路径。代码如下:
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");
}
}
}
发布于 2014-07-30 16:39:43
如果用户在搜索文件时更改了目录,则对话框将当前目录还原为其原始值,则为
;否则为false。
更新:稍微修改了我的答案,因为我认为我可能误解了你的意图:
如果我没记错的话,每次打开对话框时都会打开fd.InitialDirectory
,因为这是fd
新实例的默认定义。我认为这可能是您的问题所在:您每次试图打开它时都会调用fd = new OpenFileDialog();
。
如果您每次都将代码更改为对fd
使用相同的实例(在外部作用域中定义它,例如通过属性访问单例实例?),它可能会记住它以前的目录本身-这是默认行为(您可以使用RestoreDirectory
属性覆盖它)。
获取单例实例的示例:这将仅实例化一次对话框,并在每次调用该属性时返回相同的实例:
Private OpenFileDialog _fd;
private OpenFileDialog SingleFd {
get { return _fd ?? (_fd = new OpenFileDialog()); }
}
// Now in your method, use:
var singleInstance = SingleFd;
发布于 2014-07-30 17:12:35
fd
,但在else部分中,您使用的是变量openFileDialog
。我不明白你为什么要这么做,或者可能是打字错误。如果您希望在每次调用都取消创建实例的dialog."C:\"
,请在这两处使用相同的变量,创建一次实例,并将其用于后续调用。关于目录恢复,如果我们不设置初始目录,默认情况下它会恢复以前的目录,即使是不同的实例。
发布于 2014-07-30 17:40:20
OpenFileDialog
最初使用的是当前目录,这是一个进程范围的设置。您已通过设置InitialDirectory
覆盖了此行为。只要不这样做,它就会起作用。
如果您希望在进程重新启动时保留最后使用的目录,请捕获Environment.CurrentDirectory
并保存它。在打开对话框之前进行设置。
请注意,当前目录是进程范围的设置,因此应用程序的不同部分可能会发生冲突。此外,所有相对路径都是相对于此目录进行解释的(这就是为什么相对路径通常是一个等待错误的原因)。
https://stackoverflow.com/questions/25031823
复制相似问题