2

I have a OpenFileDialog and I am trying to set the default folder. Initially I had it set to Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\\new_folder1" and that worked well. However I changed it to Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\\new_folder2" and it still pops up in new_folder1. When I debug it, the dialog's InitialDirectory is new_folder2. I deleted new_folder1, but the dialog still looks for it when it starts up. There are now no references anywhere in my code to new_folder1.

Any ideas as to what might be happening?

Edit: Here is the code where I set up my initial OpenFileDialog:

 OpenFileDialog dlg = new OpenFileDialog();
 dlg.Filter = "XML files (*.xml)|*.xml";
 String pathDefault = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\\new_folder2";
 dlg.InitialDirectory = pathDefault;
3
  • 1
    Can you post the actual code where you set the property? Commented Feb 14, 2013 at 21:37
  • 2
    Also, it's best practice to use Path.Combine when concatenating two folders together. Commented Feb 14, 2013 at 21:37
  • 1
    And why are you using a double backslash? Just use Path.Combine to avoid making mistakes like that. Commented Feb 14, 2013 at 21:38

1 Answer 1

1

You're using @"\\....". Either get rid of the @ or change the \\ to \.

Or, try:

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),"new_folder2")
Sign up to request clarification or add additional context in comments.

3 Comments

That works. Thanks. I still don't understand why it was searching for new_folder1, or why when the default was Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\\new_folder1" it worked fine.
Yeah that is strange - I don't know either.
Looks like OpenFileDialog.InitialDirectory will accept an invalid path like C:\Users\fred\Documents\\new_folder1 (notice the double backslash). Either OpenFileDialog or the underlying common dialog is hiding the error that would occur on navigating to that directory. This would explain why the common dialog state doesn't change to the invalid directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.