Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mono c# get application path

Tags:

c#

mono

I am looking to get the directory of my application it seems to be different from regular c#?

As in Path.GetDirectoryName(Application.ExecutablePath) is not working.

like image 851
flyboarder Avatar asked Jun 05 '11 21:06

flyboarder


2 Answers

One correct and cross-platform solution would be

Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)

Note that both Environment.CurrentDirectory and Assembly.GetExecutingAssembly().Location (more exactly, the directory thereof) are semantically wrong even though they are often - but not always - the same directory:

  • The current directory is the "working directory" and can be changed at any point in time, like the "cd" command does in a shell.
  • The executing assembly is the assembly that contains the code which is currently running, and may or may not be in the same directory as the actual application. For instance if your application is "A.exe" which has a dependency "B.dll", and some code in B.dll calls Assembly.GetExecutingAssembly(), it would result in "/path/to/B.dll".
like image 107
AndiDog Avatar answered Sep 24 '22 21:09

AndiDog


Try Assembly.GetExecutingAssembly().Location

like image 30
Paul Creasey Avatar answered Sep 25 '22 21:09

Paul Creasey