gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:55 25 4
gpt4 key购买 nike

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

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

最佳答案

documentation状态:

true if the dialog box restores the current directory to its original value if the user changed the directory while searching for files; otherwise, 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;

关于c# - 使用 OpenFileDialog/C#/.Net 保留最后选择的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25031823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com