Julie Lerman's DevLife

DevLife Part I [May 2005 - March 2007]

My Links

Blog Stats

News

A blog for DevSource.com.

This blog was originally part of the blogs.ziffdavis.com site from May 2005 through June 2007 when the blog was moved to the Movable Type blog engine and hosted at blog.devsource.com/devlife.
The original blog was eventually shut down and I was given the posts so that I could host them on my own site.


Archives

Watch those default paths in Windows Forms

Did you know that using a FileOpenDialog or FileSaveDialog box can change the default path of your Windows Forms application? And in doing so, the next call to find a file in the default path could fail if you are not accessing the default path in an explicit manner. For example, just trying to open the file "mypicture.jpg" without referencing the path in any way will default to whatever happens to be the current directory. Most likely in a very controlled environment, that will be the same path that the application started up in.

You can see this happen by checking System.IO.Directory.GetCurrentDirectory before and after showing one of these dialog boxes.

It is a good reminder to be explicit about paths in your applications. There are lots and lots of things that can affect it and ClickOnce will add even another layer of complexity to this.

In the FileDialog documentation there is some explanation of some of the considerations and possible issues surrounding paths. It also provides guidance for which methods of accessing paths you would choose in a variety of scenarios.

Why am I writing about this? Because an application that has been in production for over a year just pointed out to me an instance where I neglected to follow this great advice of mine because the moon and stars just happened to be aligned in the right way to expose this little bug of mine.

posted on Sunday, March 05, 2006 10:22 PM