Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

application location 1

Status
Not open for further replies.

dinger2121

Programmer
Sep 11, 2007
439
0
0
US
Hello,
This is probably a simple answer, but how do I get the location of the current application?
for example, I want to create a simple log file to accompany my application, and I want it to alway be created in the same folder as the application. How can I get this value?

Thanks
 
The System.Reflection namespace is usually helpful for things like this.

Check out
Code:
System.Reflection.Assembly.GetExecutingAssembly().Location

This will return your exe's file name, so you will need to remove that to get your folder name. Something like:

Code:
String s = System.Reflection.Assembly.GetExecutingAssembly().Location;

String folderName = s.Substring(0, s.LastIndexOf('\\') + 1)

Hope it helps,

Alex

[small]----signature below----[/small]
Numbers is hardly real and they never have feelings but you push too hard, even numbers got limits
Why did one straw break the camel's back? Here's the secret: the million other straws underneath it

My Crummy Web Page
 
This also tells you where the .exe was started from..

System.Environment.CurrentDirectory

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Writing in the directory where the application is installed might go wrong because non-admin users don't have permission to write in the Program Files directory or in a subdirectory of Program Files.

Consider writing in the Application Data directory.

Code:
string dirname = System.Environment.GetFolderPath(SpecialFolder.ApplicationData);

Marcel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top