如何在 C# 中将 OpenFileDIalog 上的初始目录设置为用户“下载”文件夹

问题描述 投票:0回答:6

好的,我有一个 OpenFileDialog,我想将初始目录设置为用户的“下载”文件夹。这是一个内部应用程序,因此我确信用户将使用 Windows 7。

var ofd = new OpenFileDialog();

//This doesn't work
ofd.InitialDirectory =
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "Downloads");

//This doesn't work either
ofd.InitialDirectory = @"%USERPROFILE%\Downloads";

ofd.Filter = "Zip Files|*.zip";

ofd.ShowDialog();

txtFooBar.Text = ofd.FileName;

到目前为止我已经尝试过上述方法,但都不起作用。没有抛出异常,它只是没有将初始目录设置为下载文件夹。

我哪里出错了?

谢谢

c# openfiledialog
6个回答
14
投票

我可以使用环境直接调用,但我必须在末尾添加

ToString()
。直到我添加它才起作用。

saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

6
投票

也许这会有所帮助:https://stackoverflow.com/a/1175250/333404

更新

对我有用: https://stackoverflow.com/a/3795159/333404

  private void Button_Click_1(object sender, RoutedEventArgs e) {
            var ofd = new OpenFileDialog();
            ofd.InitialDirectory = GetDownloadsPath();
            ofd.Filter = "Zip Files|*.zip";
            ofd.ShowDialog();
        }

        public static string GetDownloadsPath() {
            string path = null;
            if (Environment.OSVersion.Version.Major >= 6) {
                IntPtr pathPtr;
                int hr = SHGetKnownFolderPath(ref FolderDownloads, 0, IntPtr.Zero, out pathPtr);
                if (hr == 0) {
                    path = Marshal.PtrToStringUni(pathPtr);
                    Marshal.FreeCoTaskMem(pathPtr);
                    return path;
                }
            }
            path = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
            path = Path.Combine(path, "Downloads");
            return path;
        }

        private static Guid FolderDownloads = new Guid("374DE290-123F-4565-9164-39C4925E467B");
        [DllImport("shell32.dll", CharSet = CharSet.Auto)]
        private static extern int SHGetKnownFolderPath(ref Guid id, int flags, IntPtr token, out IntPtr path);

1
投票

“Downloads”文件夹具有本地化名称,无论如何,假设众所周知的文件夹的特定相对位置(即使有详细记录)从来都不是一个好主意,因为它也可能会被用户设置更改。 不幸的是,

SpecialFolder

枚举不包含每个已知的文件夹,因此您必须使用一点互操作性,请参阅

MSDN
。从该页面我们可以找到已知文件夹的完整列表,您要查找的是FOLDERID_Downloads,因为SHGetKnownFolderPath函数需要一个GUID,您必须在某处声明该常量。你的代码将是这样的: static class ShellHelpers { public static string GetDownloadsFolder() { string path; int result = SHGetKnownFolderPath(FOLDERID_Downloads, 0, IntPtr.Zero, out path); if (result != NOERROR) Marshal.ThrowExceptionForHR(result); // You may fallback to another method or folder return path; } private static readonly Guid FOLDERID_Downloads = new Guid("374DE290-123F-4565-9164-39C4925E467B"); private static readonly int NOERROR = 0; [DllImport("shell32.dll", CharSet=CharSet.Unicode)] private static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out string pszPath); }

请注意,您可以使用您喜欢的 P/Invoke 签名(有人使用 StringBuilder,其他人使用 IntPtr)。


0
投票
ToString()

方法,也没有使用

Path.Combine()
private OpenFileDialog openFileDialog;
openFileDialog.InitialDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Downloads");



-1
投票


-2
投票

ofd.InitialDirectory = @"%USERPROFILE%\My Documents\Downloads";

© www.soinside.com 2019 - 2024. All rights reserved.