开发者

C# Windows Service Multiple Config Files

Quick Question

Is it possible to have more than 1 config file in a windows service? Or is there some way I can merge them at run time?

Scenario

Currently I have two config files containing the below contents. After I build and install my Windows Service, I can't get my custom XML Parser class to read the content because it keeps pointing to the wrong config file, even though I am using a few lines of code to ensure it gets the right name of the config file I need to access.

ALSO

When I navigate to the folder in which the service is installed, there is only sign of the normal APP.Config file, and no sign of the custom config file. (I have even set the normal ones properties to "Do Not Copy" and the custom ones properties to "Copy Always").

Config File Determination Code

           string settingsFile = String.Format("{0}.exe.config",
                System.AppDomain.CurrentDomain.BaseDirectory
            + Assembly.GetExecutingAssembly().GetName().Name);

CUSTOM 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>

NORMAL APP.CONFIG File

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


here is a primer on using multiple config files in .net. You can define some settings in a separate file and refer to it in the main config file.

In your case I think you can do it as this in main config file:

 <Servers file="CUSTOM_CONFIG_FILE.config" />


You have the option of moving an entire config section (the emphasis here is on section, not config section group, or config setting) to an external file (which must then obviously be present in the working directory at runtime) and referencing it via the configSource attribute, e.g.

<Servers configSource="CUSTOM_CONFIG_FILE.config" />

and CUSTOM_CONFIG_FILE.config would then look something like...

<?xml version="1.0" encoding="utf-8" ?>
<Servers>
</Servers>

The downside is that in your main app.config / web.config you need to define each configsection - as you have done - so I'm guessing that's not a limitation in your case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜