gpt4 book ai didi

c# - 如何以编程方式将 sectionGroup 添加到 web.config

转载 作者:太空宇宙 更新时间:2023-11-03 11:41:35 24 4
gpt4 key购买 nike

背景

我想将以下内容插入我的 web.config

  <sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
</sectionGroup>

(猜测我想要达到的目标没有奖品!)但我变得非常困惑。 MSDN 上的文档建议我需要创建一个 ConfurationSection 的子类,如果我想将一个添加到一个组中。我自己写了一个小的 Windows 应用程序来帮助我解决这个问题,但我并没有走得太远!这是相关代码 - 它试图仅添加“安全”部分。

private void AddElmahSectionGroup()
{
string exePath = Path.Combine(Environment.CurrentDirectory, "NameOfExe.exe");
Configuration configuration = ConfigurationManager.OpenExeConfiguration(exePath);

ConfigurationSectionGroup elmahGroup = configuration.GetSectionGroup(elmahSectionGroupName);
if (elmahGroup != null)
{
Console.WriteLine("sectionGroup with name {0} already in web.config", elmahSectionGroupName);
return;
}
elmahGroup = new ConfigurationSectionGroup();
configuration.SectionGroups.Add(elmahSectionGroupName, elmahGroup);

var securitySection = new Section { Name = "security", RequirePermission = false, Type = "Elmah.SecuritySectionHandler, Elmah" };
elmahGroup.Sections.Add("security", securitySection);

configuration.Save();
}

public class Section : ConfigurationSection
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name { get { return (String)this["name"]; } set { this["name"] = value; } }

[ConfigurationProperty("requirePermission", IsRequired = true)]
public bool RequirePermission { get { return (bool)this["requirePermission"]; } set { this["requirePermission"] = value; } }

[ConfigurationProperty("type", IsRequired = true)]
public string Type { get { return (string)this["type"]; } set { this["type"] = value; } }
}

这是生成的配置文件

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="elmah" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
<section name="security" type="ConfigEditing.Form1+ElmahLogic+Section, ConfigEditing, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</sectionGroup>
</configSections>
<elmah>
<security name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
</elmah>
</configuration>

这完全扭曲了我的瓜:-

  • 首先 section 元素的 type 是我的类的类型(派生自 ConfigurationSection)
  • 其次,通过将 sectionGroup 定义添加到组中,我(同时)将组添加到配置中。

我对这些发现并不感到惊讶,因为我真的不了解 API,但我只是没有找到关于我想做什么的任何体面的文档。这里的任何人都可以提供帮助 - 即使它只是指向我一个 MSDN 示例,这是一个实际的完整工作示例。

最佳答案

一个简单的示例,它将向 app.config 添加如下部分:

//<configSections>
// <sectionGroup name="elmah" type="Overflow.CustomConfigurationSectionGroup, Overflow, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" >
// </sectionGroup>
//</configSections>

namespace Overflow
{
public class CustomSecuritySection : ConfigurationSection
{
}

public class CustomConfigurationSectionGroup : ConfigurationSectionGroup
{
public CustomConfigurationSectionGroup()
{
Security = new CustomSecuritySection();
}

[ConfigurationProperty("security")]
public CustomSecuritySection Security { get; private set; }
}

class Program
{
static void Main(string[] args)
{
var config = ConfigurationManager.OpenExeConfiguration(Path.Combine(Application.StartupPath, Application.ProductName + ".exe"));

config.SectionGroups.Add("elmah", new CustomConfigurationSectionGroup());

config.Save(ConfigurationSaveMode.Modified);

}
}
}

关于c# - 如何以编程方式将 sectionGroup 添加到 web.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4618220/

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