Get Current Directory while running as WindowsService

By admin - Last updated: Monday, September 8, 2008 - Save & Share - 3 Comments

During development, while you run the application as a console application or windows form application, often the code needs to load a specific file within the same folder of the exe assembly resides. The file can be located by using the file name directory since the default location will be the exe’s directory.
For instance,


However, it is a different story if you run the application as a WindowsService. In order to resolve the excuting assembly directory path, the code needs to do a bit extra work.

Console.WriteLine(Assembly.GetExecutingAssembly().Location) ;

result: “C:\FolderName\RunMe.exe”

   Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ) );

result: “C:\FolderName”

So , load the specifc file “myfile.xml”

             Assembly.GetExecutingAssembly().Location  + @"\myfile.xml" ));

Posted in Code Notes • Tags: , , , Top Of Page

3 Responses to “Get Current Directory while running as WindowsService”

Comment from Eric Duncan
Time July 31, 2009 at 2:35 pm

Perfect, thank you. Was needing that tonight for a new service.

Comment from Josh
Time August 3, 2010 at 8:57 am

If you are going to go to the trouble to use Assembly.GetExecutingAssembly().Location to get the correct path, why not got the extra few inches and use Path.Combine to get the slashes correct, instead of doing a +@”\myfile.xml”?

Comment from Admin
Time August 3, 2010 at 9:46 am

That is really good idea to use Path.Combine

Write a comment

Captcha: one × 9 =