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 asThis PC > Documents > Visual Studio 2015 > Projects > SimpleGame > Controller > bin > Debug". Clicking in the textbox causes it to displayC:\Users\Owner\Documents\Visual Studio 2015\Projects\SimpleGame\Controller\bin\Debug. I therefore assume thatOpenFileDialogsilently handles an invalidInitialDirectoryby not changing directories. In this case, it is defaulting to my project's assembly's bin'sDebugfolder.When
dlg.InitialDirectoryis"C:\\"or"C:\\Users\\"or"C:\\Users\\Public\\"the dialog behaves as expected. Clicking in the top textbox producesC:\orC:\UsersorC:\Users\Publicrespectively.When
dlg.InitialDirectory = "C:\\Users\\Public\\Documents\\"the dialog behaves incorrectly. The top textbox shows> This PC > Documentsand the bottom textbox showsworld.txt. If I click in the top textbox, it showsDocuments. The displayed folder contents are not the same as the contents ofC:\Users\Public\Documents.Using
cmd.exelets mecdbetween the folders as expected, including intoC:\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.