我正在试图找到一种方法,在关闭OpenFileDialog之后重置初始/默认目录。请考虑以下示例:
using (OpenFileDialog openFile = new OpenFileDialog())
{
// Example: This opens in the 'Desktop' directory
// User navigates to 'Documents' directory in the Form before selecting a file
DialogResult result = openFile.ShowDialog();
if (result == DialogResult.OK) MessageBox.Show(openFile.FileName);
}
// Somewhere else, this code then runs
using (OpenFileDialog openFile = new OpenFileDialog())
{
// Problem: This now opens in 'Documents' directory. Not good!
// How to open using the same default directory (ie: Desktop)?
DialogResult result = openFile.ShowDialog();
if (result == DialogResult.OK) MessageBox.Show(openFile.FileName);
}为了说明清楚,'Desktop‘只是一个例子,我并不知道初始目录,因为它存储在注册表中(如果我正确理解的话)。
我尝试使用RestoreDirectory选项。这似乎没有任何效果。据我在其他地方所读到的,它应该将Environment.CurrentDirectory重新设置为它的原始值,这听起来是合理的。但是,我认为OpenFileDialog甚至不使用Environment.CurrentDirectory,因为值从未更改过,也不匹配OpenFileDialog打开的内容(除非我手动浏览到它)。
这里有我可能遗漏的东西吗?有人知道如何停止覆盖OpenFileDialog默认使用的目录变量吗?
发布于 2017-08-14 17:30:41
默认目录存储在Windows 7上的下列注册表项中。
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32IIRC,这可能与其他操作系统不同,因此您可能希望根据操作系统版本找到确切的目录。
因此,您可以做的是在应用程序启动时获取该值,并将其保存在内存中,并在每次打开对话框之前将其设置为OpenFileDialog.InitialDirectory。
https://stackoverflow.com/questions/45679625
复制相似问题