This post originated from an RSS feed registered with .NET Buzz
by Sean McCormack.
Original Post: Be careful with path references
Feed Title: Vini Vidi Vici - Sean McCormack's Blog
Feed URL: http://smccormack.blogs.com/adapdev/SyndicationService.asmx/GetRss
Feed Description: Sean McCormack's Blog, focusing on various aspects of .NET development, open source projects, recommended books and tools
Just came across an interesting issue. I have some code that opens a file.
Straight forward, right? The file name is declared similar to this:
string fileName ="somefile.txt";
This means that the file is relative to my current directory. I could open the
file fine when testing. A problem arose though when I used an installer,
and created a quick launch icon and a Start Menu entry. It appears that, when
you click on either of those icons, it launches the program under a different directory
than the one your program resides in. For example, if my program is in C:\Program
Files\XXX, if you launch from an icon, it will view it as something like C:\Documents
and Settings\Sean...
This means that your program will now look for somefile.txt not in the C:\Program
Files\XXX location, but rather the icon location...and won't work!
Obviously, this can cause some serious problems. To solve it, you can do one
of the following:
Change your current environment directory on startup