OpenFileDialog InitialDirectory 不起作用

发布于 2024-12-26 18:01:31 字数 716 浏览 1 评论 0原文

我有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

变身佩奇 2025-01-02 18:01:31

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.

岁月蹉跎了容颜 2025-01-02 18:01:31

对我来说这些答案没有帮助(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"

厌倦 2025-01-02 18:01:31

可能需要设置 RestoreDirectory

OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = GetDataPath(...);
dialog.RestoreDirectory = true;
dialog.AutoUpgradeEnabled = false;
dialog.Filter = GetFilter(...);
if (dialog.ShowDialog(this) == DialogResult.OK)
{...}

检查此 链接

It may require to set RestoreDirectory

OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory = GetDataPath(...);
dialog.RestoreDirectory = true;
dialog.AutoUpgradeEnabled = false;
dialog.Filter = GetFilter(...);
if (dialog.ShowDialog(this) == DialogResult.OK)
{...}

Check this link

深海里的那抹蓝 2025-01-02 18:01:31

就我而言,它不起作用,因为“InitialDirectory”不存在。

    if (!Directory.Exists(InitialDirectory))
        Directory.CreateDirectory(InitialDirectory);

In my case it was not working because the 'InitialDirectory' did not exist.

    if (!Directory.Exists(InitialDirectory))
        Directory.CreateDirectory(InitialDirectory);
只为守护你 2025-01-02 18:01:31

我也尝试过在不同地方找到的不同“解决方案”,但只要注册表中存在 MRU 列表条目,它们似乎都不起作用:/但这是我自己的简单解决方法......

而不是设置对话框的 InitialDirectory 属性,将 FileName 属性设置为您的路径,但与所选的 Filter 结合使用,例如:

dialog.FileName = Path.Combine(myPath, "*.*");

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 the FileName property to your path, but combined with the selected Filter, e.g.:

dialog.FileName = Path.Combine(myPath, "*.*");
鹿港小镇 2025-01-02 18:01:31

我得到的代码以这种方式工作:

dialog.InitialDirectory = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\Videos";

I got the code to work this way:

dialog.InitialDirectory = Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\Videos";

无风消散 2025-01-02 18:01:31

我已经尝试了给出的解决方案,但没有成功,但对我有用的是从我的路径中删除尾随的“/”。

path = path.TrimEnd(new char[] { '\\' });

然后它就可以正常工作了。

I've tried the solutions given but without success, but what worked for me, is to remove the trailing "/" from my path.

path = path.TrimEnd(new char[] { '\\' });

Then it works correctly.

骑趴 2025-01-02 18:01:31

请在发送 InitialDirectory 之前包含此函数。

public static string NormalizePath(string path)
{
    if (path != "")
    {
        return Path.GetFullPath(new Uri(path).LocalPath)
                   .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
                   .ToUpperInvariant();
    }
    else
    {
        return "";
    }
}

Please, include this function before sending the InitialDirectory.

public static string NormalizePath(string path)
{
    if (path != "")
    {
        return Path.GetFullPath(new Uri(path).LocalPath)
                   .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
                   .ToUpperInvariant();
    }
    else
    {
        return "";
    }
}
鹊巢 2025-01-02 18:01:31

我也有同样的问题。
当我使用此代码时:

string imgPath = AppDomain.CurrentDomain.BaseDirectory + @"Images\";

不显示初始目录。

但是,如果我删除了最后一个斜杠:

string imgPath = AppDomain.CurrentDomain.BaseDirectory + @"Images";

那么就开始正确显示初始目录。
恢复反斜杠不会导致不正确的显示,我不明白,但事实就是如此。

I had the same problem.
When I used this code:

string imgPath = AppDomain.CurrentDomain.BaseDirectory + @"Images\";

That does not show the initial directory.

But if I removed the final slash:

string imgPath = AppDomain.CurrentDomain.BaseDirectory + @"Images";

So began show initial directory correctly.
Restoring backslash not cause incorrect show, what I don't understand, but it is so.

没企图 2025-01-02 18:01:31

我也遇到了一个问题,它只显示最后使用的目录。我使用的是没有驱动器号的网络路径。我需要在服务器名称前面添加另一个“\”。

这不起作用:

openFileDialog1.InitialDirectory = "\\\servernam01\\group.data\\EXTERNAL PROJECTS\\VSCHART\\ercotfiles\\";

但这确实起作用:

openFileDialog1.InitialDirectory = "\\\\servernam01\\group.data\\EXTERNAL PROJECTS\\VSCHART\\ercotfiles\\";

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:

openFileDialog1.InitialDirectory = "\\\servernam01\\group.data\\EXTERNAL PROJECTS\\VSCHART\\ercotfiles\\";

But this did work:

openFileDialog1.InitialDirectory = "\\\\servernam01\\group.data\\EXTERNAL PROJECTS\\VSCHART\\ercotfiles\\";
晨与橙与城 2025-01-02 18:01:31

这发生在我身上,但问题不同。我用于 InitialDirectory 的路径有一个拼写错误。当我解决这个问题时,我很好。如果您遇到这种情况,请检查输出窗口:

A first chance exception of type 'System.IO.FileNotFoundException' 
occurred in System.Windows.Forms.dll

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:

A first chance exception of type 'System.IO.FileNotFoundException' 
occurred in System.Windows.Forms.dll
深爱成瘾 2025-01-02 18:01:31

其他答案都不适合我。请参阅下文,

GetFolderPath 用于 OpenFileDialog 对象的 InitialDirectory

using (this.openFile)
{
    this.openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
}

this.openFile 只是添加到表单中的 OpenFileDialog 对象,而不是在代码中创建新对象。

None of the other answers worked for me. See below

Use GetFolderPath for OpenFileDialog object's InitialDirectory.

using (this.openFile)
{
    this.openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
}

this.openFile is just a OpenFileDialog object added to form instead of creating a new object in code.

北笙凉宸 2025-01-02 18:01:31

CommonOpenFileDialog 似乎也有同样的问题:

展开任何未解析的环境变量,例如。 %appdata% 为完整形式:

var dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = Environment.ExpandEnvironmentVariables(path);

CommonOpenFileDialog can appear to have this same problem:

Expand any unresolved environment variables, eg. %appdata% to full form:

var dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = Environment.ExpandEnvironmentVariables(path);
白衬杉格子梦 2025-01-02 18:01:31

使用 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\

ぃ弥猫深巷。 2025-01-02 18:01:31

我也一直遇到这个问题。以下是我修复它的方法:

假设 bakDir 是一个字符串,其中包含您想要用于 OpenFileDialog 的初始目录路径。

        OpenFileDialog openFile = new OpenFileDialog();
        if (!Directory.Exists(bakDir))
        {
            Directory.CreateDirectory(bakDir);
        }
        openFile.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"DbBackups";

当您完成对所选文件的操作后,请调用以下命令:

        openFile = null;

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.

        OpenFileDialog openFile = new OpenFileDialog();
        if (!Directory.Exists(bakDir))
        {
            Directory.CreateDirectory(bakDir);
        }
        openFile.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory + @"DbBackups";

And when you're done doing your thing with selected file, call this:

        openFile = null;
肤浅与狂妄 2025-01-02 18:01:31

我解决了工作,在应用程序代码中将其放入

oFolderBrowserDialog

   .ShowNewFolderButton = True
   .RootFolder = Environment.SpecialFolder.DesktopDirectory

      If Directory.Exists(RutaReteDescarga) Then

           .SelectedPath = RutaReteDescarga
           .RootFolder = Environment.SpecialFolder.DesktopDirectory
            oFolderBrowserDialog.ShowDialog(Me)
            dRuta = .SelectedPath

    END IF

END

两次 DesktopDirectory,当我打开按钮时,默认路径出现在 Windows 中,我工作得很好。

I SOLVEDD WORK, IN THE APLICATION CODE PUT THIS¡¡¡¡¡¡¡¡¡¡¡¡¡¡

With oFolderBrowserDialog

   .ShowNewFolderButton = True
   .RootFolder = Environment.SpecialFolder.DesktopDirectory

      If Directory.Exists(RutaReteDescarga) Then

           .SelectedPath = RutaReteDescarga
           .RootFolder = Environment.SpecialFolder.DesktopDirectory
            oFolderBrowserDialog.ShowDialog(Me)
            dRuta = .SelectedPath

    END IF

END WITH

TWO TIMES DesktopDirectory, I WORK PERFECTLY WHEN I OPEN THE BUTTON THE PATH FOR DEFAULT APPEARS HERE IN THE WINDOWS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文