Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path to execution directory of Windows Forms application

I would like to get the path to the execution directory of a Windows Forms application. (That is, the directory in which the executable is located.)

Does anyone know of a built-in method in .NET to do this?

like image 940
Yaakov Ellis Avatar asked Nov 17 '08 14:11

Yaakov Ellis


People also ask

How do you reference a Windows Form?

Forms as a reference in your project. Right-click on 'References' , select 'Add Reference' and look under Assemblies in the dialogue. If you created your project as a Windows Forms project, that reference should have been added for you automatically.


2 Answers

In VB.NET

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

In C#

string directory = AppDomain.CurrentDomain.BaseDirectory; 
like image 74
Tomas Pajonk Avatar answered Sep 20 '22 16:09

Tomas Pajonk


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

Also this should give you the location of the assembly

AppDomain.CurrentDomain.BaseDirectory 

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 :/)

like image 22
thmsn Avatar answered Sep 23 '22 16:09

thmsn