Home > Development > OpenFileDialog InitialDirectory not working properly

OpenFileDialog InitialDirectory not working properly

As it’s been a while since I did some real Windows development I started building a tool that makes it easier to manage content types during a development cycle (changes to content types, changes of documententation to those content types, linked list definitions, …) One of the required abilities is to be able to load an xml file *duh*. As I wanted people to get to the initial directory of the application I used:

OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Filter “XML Files|*.xml”;
ofd.InitialDirectory System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
(ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)

To my great surprise, that did not work. I looked around on the web but didn’t really find a solution, while in debug the folder was giving the correct information. However, when I changed it into:

ofd.InitialDirectory = Environment.CurrentDirectory;

everything worked fine. I guess it’s a bit picky on how it gets the folder information…

Categories: Development Tags: ,
  1. tom
    April 26, 2011 at 3:00 pm

    Hi, thanks for your post. it seems to me that InitialDirectory must follow very strict convention for directory separation. For example,
    C:\program\loc worked, but
    C:\\program\loc didn’t work (double back slash)
    nor c:\program\loc\ (ending with slash)

    I’m not 100% certain, but this seems to be the behavior. Needs to be single slash, and it must not end with slash. cheers.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: