Posts Tagged ‘WPF’

OpenFileDialog InitialDirectory not working properly

July 30, 2009 1 comment

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: ,