OpenFileDialog InitialDirectory 不起作用
我有这样的代码:
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = GetDataPath(...);
dialog.AutoUpgradeEnabled = false;
dialog.Filter = GetFilter(...);
if (dialog.ShowDialog(this) == DialogResult.OK)
{...}
我希望在每次运行时,对话框都位于同一文件夹 - GetDataPath(...) 文件夹中,但它保留在最后选择的文件夹中。
这是正确的行为吗?你知道如何解决这个问题吗?如果 Windows 在注册表中保存了上次使用的路径,您知道如何找到它吗?
编辑1:
使用:
dialog.AutoUpgradeEnabled = true;
按预期工作...
编辑2:与此处相同的问题 在 Windows 7 中使用 SaveFileDialog 的 InitialDirectory 属性是否存在任何已知问题?
I have this code:
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = GetDataPath(...);
dialog.AutoUpgradeEnabled = false;
dialog.Filter = GetFilter(...);
if (dialog.ShowDialog(this) == DialogResult.OK)
{...}
I expect, at every run, to have the dialog in same folder - GetDataPath(...) folder, but it remains in the last selected folder.
Is this the correct behavior? Do you know how to fix this? If Windows saves last used path in registry do you know how to find it?
EDIT1:
With:
dialog.AutoUpgradeEnabled = true;
is working as expected...
EDIT2: same problem as here Any known problems with getting SaveFileDialog's InitialDirectory property working in Windows 7?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
不要将文件名包含到 InitialDirectory 中。仅路径。
来自 msdn< /a>:在 Windows Vista 上,如果将 InitialDirectory 设置为完整文件名而不仅仅是目录路径,则初始目录将默认为应用程序路径或用户上次选择文件的目录。
Do no include filename to InitialDirectory. Path only.
From msdn: On Windows Vista, if InitialDirectory is set to a full file name instead of just a directory path, the initial directory will default either to the application path, or to the directory from which the user last selected a file.
对我来说这些答案没有帮助(Windows 7)。
我的路径如下所示:“C:/xxxx/yyyyy”
切换到反斜杠后工作正常,我的路径现在看起来像这样:“C:\xxxxx\yyyyy”
to me those answers didn't help (windows 7).
my path looked like this: "C:/xxxx/yyyyy"
after switching to backslash it worked fine, my path now looks like this: "C:\xxxxx\yyyyy"
可能需要设置
RestoreDirectory
检查此 链接
It may require to set
RestoreDirectory
Check this link
就我而言,它不起作用,因为“InitialDirectory”不存在。
In my case it was not working because the 'InitialDirectory' did not exist.
我也尝试过在不同地方找到的不同“解决方案”,但只要注册表中存在 MRU 列表条目,它们似乎都不起作用:/但这是我自己的简单解决方法......
而不是设置对话框的
InitialDirectory
属性,将FileName
属性设置为您的路径,但与所选的Filter
结合使用,例如:I too have tried different "solutions" found in different places, but none of them seem to work as soon as there is an MRU list entry in the registry :/ But here is my own simple workaround…
Instead of setting the dialog's
InitialDirectory
property, set theFileName
property to your path, but combined with the selectedFilter
, e.g.:我得到的代码以这种方式工作:
dialog.InitialDirectory = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\Videos";
I got the code to work this way:
dialog.InitialDirectory = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\Videos";
我已经尝试了给出的解决方案,但没有成功,但对我有用的是从我的路径中删除尾随的“/”。
然后它就可以正常工作了。
I've tried the solutions given but without success, but what worked for me, is to remove the trailing "/" from my path.
Then it works correctly.
请在发送
InitialDirectory
之前包含此函数。Please, include this function before sending the
InitialDirectory
.我也有同样的问题。
当我使用此代码时:
不显示初始目录。
但是,如果我删除了最后一个斜杠:
那么就开始正确显示初始目录。
恢复反斜杠不会导致不正确的显示,我不明白,但事实就是如此。
I had the same problem.
When I used this code:
That does not show the initial directory.
But if I removed the final slash:
So began show initial directory correctly.
Restoring backslash not cause incorrect show, what I don't understand, but it is so.
我也遇到了一个问题,它只显示最后使用的目录。我使用的是没有驱动器号的网络路径。我需要在服务器名称前面添加另一个“\”。
这不起作用:
但这确实起作用:
I had a problem with this too where it only would show the last directory used. I was using a network path with no drive letter. I needed to add another "\" in front of the server name.
This didn't work:
But this did work:
这发生在我身上,但问题不同。我用于
InitialDirectory
的路径有一个拼写错误。当我解决这个问题时,我很好。如果您遇到这种情况,请检查输出窗口:This was happening to me, but the problem was different. I had a typo in the path I was using for the
InitialDirectory
. When I fixed that, I was fine. If this is happening to you check your output window for this:其他答案都不适合我。请参阅下文,
将
GetFolderPath
用于OpenFileDialog
对象的InitialDirectory
。this.openFile
只是添加到表单中的OpenFileDialog
对象,而不是在代码中创建新对象。None of the other answers worked for me. See below
Use
GetFolderPath
forOpenFileDialog
object'sInitialDirectory
.this.openFile
is just aOpenFileDialog
object added to form instead of creating a new object in code.CommonOpenFileDialog
似乎也有同样的问题:展开任何未解析的环境变量,例如。
%appdata%
为完整形式:CommonOpenFileDialog
can appear to have this same problem:Expand any unresolved environment variables, eg.
%appdata%
to full form:使用 Path.GetFullPath() 对我有用
dialog.InitialDirectory =Path.GetFullPath (Environment.CurrentDirectory + @"....\Data\Tabs");
返回 G:\Arduino\GUI\Piano\Piano\Data\Tabs\
而不是 G:\Arduino\GUI\Piano\Piano\bin\Debug....\Data\Tabs\
Using Path.GetFullPath() worked for me
dialog.InitialDirectory =Path.GetFullPath (Environment.CurrentDirectory + @"....\Data\Tabs");
which returns G:\Arduino\GUI\Piano\Piano\Data\Tabs\
instead of G:\Arduino\GUI\Piano\Piano\bin\Debug....\Data\Tabs\
我也一直遇到这个问题。以下是我修复它的方法:
假设 bakDir 是一个字符串,其中包含您想要用于 OpenFileDialog 的初始目录路径。
当您完成对所选文件的操作后,请调用以下命令:
I have been having issues with this too. Here is how I fixed it:
Assume bakDir is a string containing the initial directory path you want for your OpenFileDialog.
And when you're done doing your thing with selected file, call this:
我解决了工作,在应用程序代码中将其放入
oFolderBrowserDialog
END
两次 DesktopDirectory,当我打开按钮时,默认路径出现在 Windows 中,我工作得很好。
I SOLVEDD WORK, IN THE APLICATION CODE PUT THIS¡¡¡¡¡¡¡¡¡¡¡¡¡¡
With oFolderBrowserDialog
END WITH
TWO TIMES DesktopDirectory, I WORK PERFECTLY WHEN I OPEN THE BUTTON THE PATH FOR DEFAULT APPEARS HERE IN THE WINDOWS.