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?
By default, the current directory for your Windows service is the System32 folder.
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.
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.
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.
Try this:
System.Reflection.Assembly.GetEntryAssembly().Location
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With