How to set the source directory in OpenFileDIalog to the `Downloads` folder of users in C #

So, I have OpenFileDialog, and I want to set the initial directory in the "Download" folder of users. This is an internal application, and therefore I am sure that the user will use 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;

I have already tried this and am not working. No Exception is not thrown, it just does not set the source directory to the download folder.

Where am I going wrong?

thank

+6
source share
4 answers

Maybe this could help: fooobar.com/questions/177864 / ...

UPDATE

Works for me: fooobar.com/questions/283509 / ...

  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);
+5
source

, ToString() . , .

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

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

, ( ), .

The enumeration failure SpecialFolderdoes not contain every known folder, so you need to use a little interaction, see MSDN . On this page we can find a complete list of known folders , what you are looking for is FOLDERID_Downloads , because the SHGetKnownFolderPath Function requires a GUID, which you must declare where something with this constant. Your code will look something like this:

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

Please note that you can use the P / Invoke signature that you prefer (some use StringBuilder and the other IntPtr).

0
source

All Articles