1

I'm developing a Windows service and I have added a special configuration XML file into it. Data in that file can be read by the service when debugging with Visual Studio. But when the file is built, that XML file is not added to the build folder. Also after installing the service, It is starting from System32 (default location) It gives an error (Cannot find the file C:\Windows\System32\ServerAgent.xml) File is not added to the installation folder.

How to add my XML file to the build folder and installation location?

Thanks in advance.

JayNaz
  • 343
  • 1
  • 5
  • 24
  • 2
    In the properties of the file in the VisualStudio solution explorer, you can specify whether or not the file shall be copied to the output directory. When loading the file in your service, do not rely on the current dirctory, but specify the full path. – Klaus Gütter Apr 05 '20 at 05:03

1 Answers1

3
  1. In Visual Studio, click on the file in the file tree
  2. Right click and choose Properties
  3. There is a property called "Copy to Output Directory"
  4. Select "Copy Always" or "Copy if Newer"

It would be best to specify the full path to the xml file with your app settings. See this answer to find the folder that your application is in:

https://stackoverflow.com/a/8081812/65432

Rosco
  • 2,108
  • 17
  • 17
  • Thank you @Rosco Your solution solved a part of my problem. Now the xml file is added to the build folder. But when the service is starting (Not installing) it's searching for that file in `System32` folder. Getting the same error.. – JayNaz Apr 05 '20 at 06:55
  • The service need to start the application executable in a location where the xml is in the same folder. – jdweng Apr 05 '20 at 11:52
  • @JayNaz Added more about finding the location of the xml file – Rosco Apr 05 '20 at 12:06
  • The file not found error was fixed by this. I replaced `doc.Load(Directory.GetCurrentDirectory() + @"\ServerAgentConfig.xml");` with `doc.Load(AppDomain.CurrentDomain.BaseDirectory + @"\ServerAgentConfig.xml");` Simple as that. Thank you @Rosco – JayNaz Apr 09 '20 at 16:33