开发者

C# Windows Service XML

Scenario

I have a windows service written in C# that performs some processing based on parsing an XML file and use that data to carry out various tasks. The service also does various bits of logging - which uses settings from an APP.Config file.

The Problem

When the service is compiled, installed and run, the XML file seems to disappear. I'm getting the impression that it is just ignored or something like that.

So far

I've tried using TWO App.Config files, one named App.Config that contains settings for the service, and the other called MyService.exe.config that contains all of the data that was used in the XML file (the idea being that I can parse the XML from a config file that actually gets compiled and appears in my installation directory.

However

When I do this, all that happens is that ONE config file appears (with the name MyService.exe.config), but it contains the contents of the App.Config file and not the XML data that I want to parse.

What I need

All I want is to have a config file for my settings, and an XML file for my data.

Question

Is this possible? I know the application works as it was originally built as a console application that ran fine.

Other

The application has to be designed this way (as in, I need my data stored as XML, and my settings stored in a config file).

Thoughts

If I could somehow combine the contents of the two files into ONE config file, that would be one way of solving the problem. However, I have tried this and of course I get a "Type Initialisation Exception", as the config file cannot interprate the XML data (probably because the tags are custom and do not form any part of the config schema - or something like that).

Ideas

Please could someone explain to me if it is possible for me to have an XML file AND a config file that will actually be compiled and stored in my installation directory for the service when it is run?

CODE

Custom XML/Data Config File

   <?xml version="1.0" encoding="utf-8" ?> 
  <configuration> 
  <servers> 
<SV066930> 
  <add name="Name" value = "SV066930" /> 
  <processes> 
<SimonTest1> 
  <add name="ProcessName" value="notepad.exe" /> 
  <add name="CommandLine" value="C:\\WINDOWS\\system32\\notepad.exe    C:\\WINDOWS\\Profiles\\TA2TOF1\\Desktop\\SimonTest1.txt" /> 
          </SimonTest1> 
 </processes> 
  </SV066930> 
  </servers> 
 </configuration> 

APP.Config Settings File

   <?xml version="1.0" encoding="utf-8" ?> 
  开发者_Go百科<configuration> 
   <configSections> 
     <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxx" /> 
   </configSections> 
  <connectionStrings> 
 <add name="DB" connectionString="Data Source=etc......" /> 
  </connectionStrings> 
  </configuration> 

Help greatly appreciated.


You haven't said what the build action for your XML file is. You can either make it Content, in which case it should be copied to the output directory, or you could make it an Embedded Resource, in which case it will be embedded within your assembly.

If you don't need to change it after the build, I'd go for the Embedded Resource approach - it means that you don't have another file to worry about. However, it's less flexible as you can't change the file afterwards. For this option, you'd use Assembly.GetManifestResourceStream to load the XML file at execution time.

If you go for the Content approach, be aware that the current directory of your process may not be the one containing your assembly - you can use

string module = typeof(YourService).Assembly.GetModules()[0].FullyQualifiedName;
string directory = Path.GetDirectoryName(module);

to find the directory, and then load the file relative to that.


The file named App.config in you C# project gets renamed to MyService.exe.config when it is built so you're gonna have to rename your second config file that's all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜