gpt4 book ai didi

.net - 外部文件中的applicationSettings

转载 作者:行者123 更新时间:2023-12-03 04:16:01 24 4
gpt4 key购买 nike

我一直在尝试使用一些在 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;

最佳答案

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

设置类:

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>

关于.net - 外部文件中的applicationSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105291/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com