Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a text file from a Windows Service?

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?

like image 811
Jed Avatar asked Jun 26 '12 18:06

Jed


1 Answers

Use this:

System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Foo.xml");
like image 168
Candide Avatar answered Oct 29 '22 20:10

Candide