I have made a Windows Service that gets installed in a c:\Program Files\My Service
directory. Along with the executable, I have an XML file that gets installed in the same directory. This XML file is used by the service to get user defined information.
In the code of the service I read the file as if it were local to the executable.. example:
DataSet ds = new DataSet();
ds.ReadXml("Foo.xml");
However, when I run the service, the service throws an exception saying:
Could not find file 'C:\Windows\system32\Foo.xml'
Since the executable lives in c:\Program Files\My Service
I expected the Windows Service to look for the XML file in c:\Program Files\My Service\Foo.xml
. Obviously, that's not the case.
How can I force the service to look (relatively) for the Foo.xml file in the same location where the service executable lives?
Use this:
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Foo.xml");
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