【问题标题】:C# Windows Service Multiple Config FilesC# Windows 服务多个配置文件
【发布时间】:2011-02-08 00:47:37
【问题描述】:

快速提问

Windows 服务中是否可以有多个配置文件? 或者有什么方法可以在运行时合并它们?

场景

目前我有两个包含以下内容的配置文件。 构建和安装 Windows 服务后,我无法让我的自定义 XML Parser 类读取内容,因为它一直指向错误的配置文件,即使我使用了几行代码来确保它获得正确的名称我需要访问的配置文件。

当我导航到安装服务的文件夹时,只有普通 APP.Config 文件的迹象,而没有自定义配置文件的迹象。 (我什至将普通属性设置为“不复制”,将自定义属性设置为“始终复制”)。

配置文件判断代码

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

自定义配置文件

     <?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 文件

<?xml version="1.0" encoding="utf-8" ?>
<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>

【问题讨论】:

    标签: c# app-config windows-services


    【解决方案1】:

    您可以选择将整个配置部分(这里的重点是部分,而不是配置部分组或配置设置)移动到外部文件(然后在运行时显然必须存在于工作目录中)并引用它通过 configSource 属性,例如

    <Servers configSource="CUSTOM_CONFIG_FILE.config" />
    

    然后 CUSTOM_CONFIG_FILE.config 看起来像...

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

    缺点是在你的主 app.config / web.config 中你需要定义每个配置部分 - 正如你所做的那样 - 所以我猜这不是你的情况的限制。

    【讨论】:

      【解决方案2】:

      here 是在 .net 中使用多个配置文件的入门。您可以在单独的文件中定义一些设置,并在主配置文件中引用它。

      在你的情况下,我认为你可以在主配置文件中这样做:

       <Servers file="CUSTOM_CONFIG_FILE.config" />
      

      【讨论】:

        猜你喜欢
        • 2010-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-09
        • 2013-12-07
        相关资源
        最近更新 更多