3

我一直在尝试使用在http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.configsource%28VS.85%29.aspx上找到的一些代码来放置我的应用程序设置一个外部文件,但不能让它工作。

应用程序配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings">
          <section name="ConsoleApplication1.Settings" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <ConsoleApplication1.Settings configSource="externalSettings.config" />
    </applicationSettings>
</configuration>

外部设置.config:

<ConsoleApplication1.Settings>
  <setting name="TestSetting" serializeAs="String">
    <value>Hello</value>
  </setting>
</ConsoleApplication1.Settings>

我正在尝试像您通常那样使用设置:

var test = ConsoleApplication1.Settings.Default.TestSetting;
4

2 回答 2

0

我确实遇到了同样的问题。它已在配置中使用正确的命名空间解决

设置类:

namespace ConsoleApp1.Properties {


[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default {
        get {
            return defaultInstance;
        }
    }

    [global::System.Configuration.ApplicationScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public string TestSetting {
        get {
            return ((string)(this["TestSetting"]));
        }
    }
}
}

用法:

var test = ConsoleApp1.Properties.Settings.Default.TestSetting;

配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <section name="ConsoleApp1.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />      
</sectionGroup>
</configSections>
  <applicationSettings>
    <Settings configSource="externalSettings.config" />
  </applicationSettings>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
</configuration>

外部设置.config:

<ConsoleApp1.Settings>
<setting name="TestSetting" serializeAs="String">
 <value>Hello</value>
</setting>
</ConsoleApp1.Settings>
于 2019-05-31T14:49:05.410 回答
-1

检查这个帖子

不同 .config 文件上的 AppSettings 未更新

只需与您的解决方案进行比较

    <appSettings file="Settings.config">
    <add key="RestartApp" value="-1" />
  </appSettings>


  <appSettings>
    <add key="AppTitle" value="MVC Web Access" />
    <add key="DefaultWebpage" />
    <add key="CustomCSS" />
    <add key="TktsEmailTo" value="email@email.com" />
    <add key="EmailFrom" value="email@email.com" />
    <add key="EmailFromSMTP" value="mail.email.com" />
    <add key="EmailFromPW" value="fakePassword" />
  </appSettings>


  <appSettings file="Settings.config">
    <add key="RestartApp" value="-1" />
    <add key="AppTitle" value="New title" />
    <add key="DefaultWebpage" value="index.aspx" />
    <add key="CustomCSS" />
    <add key="TktsEmailTo" value="newemail@email.com" />
    <add key="EmailFrom" value="newemail@email.com" />
    <add key="EmailFromSMTP" value="mail.email.com" />
    <add key="EmailFromPW" value="NewFakePassword" />
  </appSettings>

我用这种方法成功地将我的 appSettings 移动到另一个文件

希望能帮助到你

于 2013-04-19T13:30:17.703 回答