The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Be careful with path references

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Sean McCormack

Posts: 504
Nickname: adapdev
Registered: Jan, 2005

Sean McCormack is the Lead .NET Architect for Miller Brewing Co. and founder of Adapdev Technologies
Be careful with path references Posted: Jan 6, 2005 2:51 PM
Reply to this message Reply

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
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Sean McCormack
Latest Posts From Vini Vidi Vici - Sean McCormack's Blog

Advertisement

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:

  1. Change your current environment directory on startup

    Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

  2. Reference your files appropriately

    string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "somefile.txt");

 

Read: Be careful with path references

Topic: Join MCE 2005 Box to Domain Previous Topic   Next Topic Topic: Microsoft MVP - .NET/C# for 2nd year

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use