views:

6685

answers:

5

I would like to get the path to the execution directory of a windows forms application (ie: the directory in which the executable is located). Does anyone know of a built-in method in .Net to do this?

+6  A: 

In VB.NET

Dim directory as String = My.Application.Info.DirectoryPath

In C#

string directory = AppDomain.CurrentDomain.BaseDirectory;
Tomas Pajonk
The C# location will also work in VB or other languages that don't support the "My" namepsace.
Joel Coehoorn
@Tomas Pajonk, may I suggest changing the c# "FileName" variable to "directory"?
grenade
@grenade done. Thanks.
Tomas Pajonk
+1  A: 

This could help;

Path.GetDirectoryName(Application.ExecutablePath);

also here is the reference

yapiskan
+3  A: 

Application.Current results in an appdomain http://msdn.microsoft.com/en-us/library/system.appdomain_members.aspx

Application.Current.BaseDirectory should give you the location of the assembly

I seem to recall there being multiple ways of getting the location of the application. but this one worked for me in the past atleast (it's been a while since i've done winforms programming :/)

thmsn
A: 
string apppath = 
    (new System.IO.FileInfo
    (System.Reflection.Assembly.GetExecutingAssembly().CodeBase)).DirectoryName;
MusiGenesis
Sorry, the Assembly namespace and the code display panel do not play nicely together. I hate scrollbars.
MusiGenesis
+1  A: 

System.Windows.Forms.Application.StartupPath will solve ur problem i think.