Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows service - get current directory

I have a Windows service that should look for a configuration file in its current directory.

So I use directory.getcurrentdirectiry() but instead of the service directory I get back

c:\windows\system32 

Any idea why and how should I get the service directory?

like image 292
MoShe Avatar asked Apr 29 '12 21:04

MoShe


People also ask

What is the current directory of a Windows service?

By default, the current directory for your Windows service is the System32 folder.

How do I display the current directory in Windows?

To list the files in the current directory use the dir command, and if you want to change the current directory, use the cd command. You can use the chdir command by itself to print the current directory in MS-DOS and the Windows command line.

What does AppDomain CurrentDomain BaseDirectory return?

AppDomain.CurrentDomain.BaseDirectoryThis path gives you the path of the Entrypoint of the application or from where the Current AppDomain is created. For instance, say your executable is executed on assembly a.exe, but the code is written in b.


2 Answers

You can set the current directory to the directory that your service is running from by including this line in your code:

System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory); 

The important part of this is:

System.AppDomain.CurrentDomain.BaseDirectory 

That returns the path to the directory your service is running from.

like image 72
Jed Avatar answered Oct 22 '22 00:10

Jed


Try this:

System.Reflection.Assembly.GetEntryAssembly().Location 
like image 21
coder Avatar answered Oct 22 '22 00:10

coder