What is the best way to get the executing exe's path in .NET?

pistacchio picture pistacchio · Aug 3, 2009 · Viewed 70k times · Source

From program a.exe located in c:/dir I need to open text file c:/dir/text.txt. I don't know where a.exe could be located, but text.txt will always be in the same path. How to get the name of the currently executing assembly from within to program itself so that i can access the text file?

EDIT: what if a.exe is a Windows service? It doesn't have Application as it is not a Windows Applicaion.

Thanks in advance.

Answer

Tim S. Van Haren picture Tim S. Van Haren · Aug 3, 2009

I usually access the directory that contains my application's .exe with:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);