How can I get CommonOpenFileDialog InitialDirectory to be a user of MyDocuments, not libraries / documents?

I am using the CommonOpenFileDialog in the Windows Code Code Pack as a folder selection dialog. I set the InitialDirectory property to Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments). However, when I show the dialog, the path in the address bar will be Libraries \ Documents (not C: \ users \ craig \ my documents, as I expected). In addition, if I just click on the “Select Folder” button, I get a dialog that says “You have selected a library. Instead, select a folder.

Does anyone know why my path to the file is ignored, in favor of "libraries \ documents"? More importantly, how can I make the dialog respect the passed value of InitialDirectory?

The code I use for dialogue is:

if (CommonFileDialog.IsPlatformSupported)
{
    var folderSelectorDialog = new CommonOpenFileDialog();
    folderSelectorDialog.EnsureReadOnly = true;
    folderSelectorDialog.IsFolderPicker = true;
    folderSelectorDialog.AllowNonFileSystemItems = false;
    folderSelectorDialog.Multiselect = false;
    folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    folderSelectorDialog.Title = "Project Location";

    if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
      ShellContainer shellContainer = null;

      try
      {
        // Try to get a valid selected item
        shellContainer = folderSelectorDialog.FileAsShellObject as ShellContainer;
      }
      catch
      {
        MessageBox.Show("Could not create a ShellObject from the selected item");
      }

      FilePath = shellContainer != null ? shellContainer.ParsingName : string.Empty;
    }
}

Thank,

-Craig

+5
source share
2 answers

First of all, I am sorry that it took me so long to understand your question.

The message I see is when I try to do this:

"Libraries \ Documents" does not work, because it is not part of the file system.

There is still nothing to say. A library is a virtual folder that combines various different folders.

There is no real way to avoid this error. You asked the dialog box to return the folder, and the user did not select the folder. Therefore, the dialogue cannot fulfill its part of the transaction.

If you go down further into the folder structure, into real folders, the dialog box will return you the real value.

+2

folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
0

All Articles