Is there a way to launch the OpenFileDialog
in the C:\Users\Public\Documents
folder?
I am writing a C# application, using the DotNet framework. I am trying to launch an OpenFileDialog
, with an InitialDirectory
of "C:\\Users\\Public\\Documents\\"
and a FileName
of "world.txt"
. Unfortunately, the OpenFileDialog
is putting me in the Documents
shortcut instead of into C:\Users\Public\Documents
.
Expected results
I expect to see the OpenFileDialog open, with the top textbox showing > This PC > Windows7_OS (C:) > Users > Public > Documents
and the bottom textbox showing world.txt
. I expect that if I click in the top textbox, it will show C:\Users\Public\Documents
.
Actual results
The OpenFileDialog opens. The top textbox shows > This PC > Documents
and the bottom textbox shows world.txt
. If I click in the top textbox, it shows Documents
. The displayed folder contents are not the same as the contents of C:\Users\Public\Documents
.
Things I have tried
I have stopped the code in the Visual Studio debugger after the following line of code:
OpenFileDialog dlg = new OpenFileDialog();
In the Immediate Window, I have executed code such as the following:
dlg.FileName = "world.txt"
? dlg.FileName
dlg.InitialDirectory = "C:\\NonExistentDirectory\\";
dlg.ShowDialog();
dlg.InitialDirectory = "C:\\";
dlg.ShowDialog();
dlg.InitialDirectory = "C:\\Users\\";
dlg.ShowDialog();
dlg.InitialDirectory = "C:\\Users\\Public\\";
dlg.ShowDialog();
dlg.InitialDirectory = "C:\\Users\\Public\\Documents\\";
dlg.ShowDialog();
I cancel out of each dialog.
I used C:\WINDOWS\System32\cmd.exe
to cd
between C:\
and C:\Users\
and C:\Users\Public
and C:\Users\Public\Documents\
.
Results of things I have tried
When dlg.InitialDirectory = "C:\\NonExistentDirectory\\"
, the dialog's folder initially displays as This PC > Documents > Visual Studio 2015 > Projects > SimpleGame > Controller > bin > Debug"
. Clicking in the textbox causes it to display C:\Users\Owner\Documents\Visual Studio 2015\Projects\SimpleGame\Controller\bin\Debug
. I therefore assume that OpenFileDialog
silently handles an invalid InitialDirectory
by not changing directories. In this case, it is defaulting to my project's assembly's bin's Debug
folder.
When dlg.InitialDirectory
is "C:\\"
or "C:\\Users\\"
or "C:\\Users\\Public\\"
the dialog behaves as expected. Clicking in the top textbox produces C:\
or C:\Users
or C:\Users\Public
respectively.
When dlg.InitialDirectory = "C:\\Users\\Public\\Documents\\"
the dialog behaves incorrectly. The top textbox shows > This PC > Documents
and the bottom textbox shows world.txt
. If I click in the top textbox, it shows Documents
. The displayed folder contents are not the same as the contents of C:\Users\Public\Documents
.
Using cmd.exe
lets me cd
between the folders as expected, including into C:\Users\Public\Documents
.
My environment
I am running Microsoft Visual Studio Community 2015 Version 14.0.23107.0 D14REL, using Microsoft Visual C# 2015. My operating system is Windows 10 Pro.
If you are using System.Windows.Forms.OpenFileDialog you can set:
dialog.AutoUpgradeEnabled = false;
The dialog will look a little dated/flat/"old school" but it does at least display the correct contents!