相关疑难解决方法(0)

如何在.NET中为自定义配置节启用configSource属性?

根据此处发现的大量信息,我们如何才能使外部.config工作?我尝试了相同的设置,我将用于外部appSettings文件,但它无法找到我的自定义部分的文件.

<configSections>
...
    <section name="CustomSettings" type="Fully.Qualified.TypeName.CustomSettings, AssemblyName" />
</configSections>
<!-- this works -->
<CustomSettings attrib1="val1" attrib2="val2" .../>
Run Code Online (Sandbox Code Playgroud)

然而...

<!--this does not work-->
<CustomSettings configSource="someExternalFile.config"/>
Run Code Online (Sandbox Code Playgroud)

someExternalFile.config包含的位置

<CustomSettings attrib1="val1" attrib2="val2" .../>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

.net c# configuration app-config configuration-files

27
推荐指数
1
解决办法
2万
查看次数

如何动态加载单独的应用程序设置文件并与当前设置合并?

有关从单独的配置文件中读取设置的问题以及与其类似的其他配置文件,但我的问题是特定于应用程序属性设置(即<MyApplication.Properties.Settings>- 请参阅下面的XML文件)以及如何动态加载它们.我在这篇文章中尝试了这个方法,其中包括刷新主配置文件的整个appSettings部分,但我的改编引发了异常,因为我没有替换appSettings部分:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
// Have tried the other ConfigurationUserLevels to no avail
config.AppSettings.File = myRuntimeConfigFilePath;
config.Save(ConfigurationSaveMode.Modified); // throws ConfigurationErrorsException
ConfigurationManager.RefreshSection("userSettings");
Run Code Online (Sandbox Code Playgroud)

ConfigurationErrorsException.Message是"根元素必须与引用文件的部分的名称匹配'appSettings'(C:\ myFile.xml第2行)." 该文件是:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <userSettings>
        <MyApplication.Properties.Settings>
            <setting name="SineWaveFrequency" serializeAs="String">
                <value>6</value>
            </setting>
            <setting name="SineWaveAmplitude" serializeAs="String">
                <value>6</value>
            </setting>
        </MyApplication.Properties.Settings>
    </userSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

有没有办法将此文件中的值导入到MyApplication.Properties.Settings.Default类中,框架处理所有XML反序列化,就像在应用程序启动时加载配置文件一样?

.net settings persistence configuration-files

9
推荐指数
1
解决办法
2万
查看次数

如何从 web.config 中提取一个部分到一个单独的文件中

我有以下部分Web.config

 <httpProtocol>
  <customHeaders>
    <remove name="X-UA-Compatible" />
    <remove name="X-Frame-Options" />
    <remove name="X-XSS-Protection" />
    <remove name="X-Content-Type-Options" />
    <add name="X-UA-Compatible" value="IE=Edge" />
    <add name="X-Frame-Options" value="DENY" />
    <add name="X-XSS-Protection" value="1; mode=block"></add>
    <add name="X-Content-Type-Options" value="nosniff" />
  </customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)

我想提取<customHeaders>到一个名为 的配置文件web.customer.customHeaders.config。为了实现这一目标,我web.customer.customHeaders.config在与我所在的位置相同的位置创建了该文件,Web.config并在其中编写了以下 XML:

<customHeaders>
    <remove name="X-UA-Compatible" />
    <remove name="X-Frame-Options" />
    <remove name="X-XSS-Protection" />
    <remove name="X-Content-Type-Options" />
    <add name="X-UA-Compatible" value="IE=Edge" />
    <add name="X-Frame-Options" value="DENY" />
    <add name="X-XSS-Protection" value="1; mode=block"></add>
    <add name="X-Content-Type-Options" value="nosniff" />
  </customHeaders>
Run Code Online (Sandbox Code Playgroud)

我还在<customHeaders>我的文件中更改了该部分,Web.config …

.net c# vb.net asp.net

5
推荐指数
1
解决办法
559
查看次数