0

我在 Visual Studio 2010 中工作,我有一个主项目和两个其他库项目,并且在主项目中添加了参考。每个项目都有一个设置文件。构建解决方案后,主项目只有一个 .config 文件,但库项目没有 .config 文件。因此,我无法从外部更改这些库项目的设置。

那么这个问题的解决方案是什么,我可以将库项目的 .config 文件放入我的调试/发布文件夹中?

应用程序配置

<?xml version="1.0"?>
<configuration>
<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <section name="Communicator.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <section name="DatabaseManager.Database.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>
<applicationSettings>
    <Communicator.Properties.Settings>
        <setting name="ApplicationConfigsDirectory" serializeAs="String">
            <value>\Application_Configs</value>
        </setting>
        <setting name="MaxMeterConn" serializeAs="String">
            <value>10000</value>
        </setting>
        <setting name="MaxPhyConn" serializeAs="String">
            <value>20000</value>
        </setting>
        <setting name="ServerIP" serializeAs="String">
            <value>0.0.0.0</value>
        </setting>
        <setting name="Port" serializeAs="String">
            <value>1114</value>
        </setting>
        <setting name="TCPTimeOut" serializeAs="String">
            <value>3900000000</value>
        </setting>
        <setting name="isTCPTimeOut" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="SchedulerPoolingTime" serializeAs="String">
            <value>480000</value>
        </setting>
        <setting name="startKAScheduler" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="MinCacheAge" serializeAs="String">
            <value>00:02:00</value>
        </setting>
        <setting name="MaxCahceAge" serializeAs="String">
            <value>00:05:00</value>
        </setting>
        <setting name="SaveLogToDBFlag" serializeAs="String">
            <value>True</value>
        </setting>
        <setting name="Instance" serializeAs="String">
            <value>MDC_Tester</value>
        </setting>
    </Communicator.Properties.Settings>

    <DatabaseManager.Database.Properties.Settings>
      <Setting Name="Server" Type="System.String" Scope="Application">
        <Value Profile="(Default)">192.168.30.181</Value>
      </Setting>
      <Setting Name="Database" Type="System.String" Scope="Application">
        <Value Profile="(Default)">tester</Value>
      </Setting>
      <Setting Name="UserID" Type="System.String" Scope="Application">
        <Value Profile="(Default)">root</Value>
      </Setting>
      <Setting Name="pswd" Type="System.String" Scope="Application">
        <Value Profile="(Default)">nothing</Value>
      </Setting>
      <Setting Name="MaxPoolSize" Type="System.Int32" Scope="Application">
        <Value Profile="(Default)">512</Value>
      </Setting>
    </DatabaseManager.Database.Properties.Settings>
</applicationSettings>
<system.runtime.caching>
<memoryCache>
  <namedCaches>
    <add name="default"
         cacheMemoryLimitMegabytes="1024"
         pollingInterval="00:01:30" />
  </namedCaches>
</memoryCache>
</system.runtime.caching>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
4

1 回答 1

3

这是设置和配置文件的预期行为。您需要将库项目中的配置部分复制到主项目中。您还需要将相应的部分元素添加到 applicationSettings 部分组:

<configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="SomeLibrary.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      <section name="MainProject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
</configSections>

然后在主配置中:

<applicationSettings>
     <SomeLibrary.Properties.Settings>
         <setting name="SomeSetting" serializeAs="String">
            <value>Whatever</value>
         </setting>
     </SomeLibrary.Properties.Settings>
     <MainProject.Properties.Settings>
        <setting name="MainSetting" serializeAs="String">
            <value>Yada yada</value>
        </setting>
    </MainProject.Properties.Settings>
</applicationSettings>

在上面,在主项目中声明了“SomeLibrary”设置类型,然后在主配置文件中设置与库相关的设置以及主项目的设置。

于 2012-12-26T08:13:39.957 回答