WebConfigurationManager和ConfigurationManager之间有什么区别?
我什么时候应该使用另一个?
更新
我只是查看了WebConfigurationManager,由于某种原因,您无法像在ConfigurationManager中那样访问连接字符串(如数组).谁能告诉我为什么MS这样做了?使用WebConfigurationManager获取所需的连接字符串似乎很痛苦.
再次更新CAVEAT!
如果您没有引用添加到项目中的"System.Configuration"命名空间,那么当您尝试访问WebConfigurationManager.ConnectionStrings时,Visual Studio将显示错误!
.net c# asp.net configurationmanager webconfigurationmanager
我有一个在IIS7中创建网站的win表单.一个函数需要打开web.config文件并进行一些更新.(连接字符串,smtp,模拟)
但是我没有虚拟路径,只有物理路径.
有什么办法我还可以使用WebConfigurationManager吗?
我需要使用它来查找节和读/写的能力.
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration
我在使用ASP.NET网站的测试服务器上遇到了一些麻烦.我搞砸了,并且默认网站的主目录指向了错误的位置.当我尝试:
ConfigurationManager.ConnectionStrings["connectionString"]; 
它返回null,但是
using System.Web.Configuration;
/* ... */
var rootWebConfig =
    WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
rootWebConfig.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString;` 
返回正确的连接字符串.
这两种方法之间有什么区别?
编辑:我真正要问的是,为什么ConfigurationManager在主目录设置不正确时方法失败,否则成功,并且WebConfigurationManager无论主目录是否正确设置都成功?是否存在其他差异,例如关于访问控制的假设?
c# asp.net configurationmanager behavior webconfigurationmanager
我有一个控制台应用程序试图从web.config文件加载CustomConfigurationSection.
自定义配置部分具有所需的自定义配置元素.这意味着当我加载配置部分时,如果配置中不存在该配置元素,我希望看到异常.问题是.NET框架似乎完全忽略了isRequired属性.因此,当我加载配置部分时,我只创建自定义配置元素的实例并将其设置在配置部分.
我的问题是,为什么会发生这种情况?我希望GetSection()方法触发ConfigurationErrors异常,因为配置中缺少必需的元素.
以下是我的配置部分的外观.
public class MyConfigSection : ConfigurationSection
{
    [ConfigurationProperty("MyConfigElement", IsRequired = true)]
    public MyConfigElement MyElement
    {
        get { return (MyConfigElement) this["MyConfigElement"]; }
    }
}
public class MyConfigElement : ConfigurationElement
{
    [ConfigurationProperty("MyAttribute", IsRequired = true)]
    public string MyAttribute
    {
        get { return this["MyAttribute"].ToString(); }
    }
}
这是我加载配置部分的方法.
   class Program
    {
        public static Configuration OpenConfigFile(string configPath)
        {
            var configFile = new FileInfo(configPath);
            var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
            var wcfm = new WebConfigurationFileMap();
            wcfm.VirtualDirectories.Add("/", vdm);
            return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
        } …c# configuration configurationsection configurationelement webconfigurationmanager
我之前已多次使用过这两个配置文件,但我从未花时间充分了解它们的工作原理.正如大多数人所做的那样,我理解了如何调用WebConfigurationManager.AppSettings["key"]获取配置值的基础知识.
以下是我提出的一些问题:
c# configurationmanager appsettings application-settings webconfigurationmanager
我正在使用 C# 和 .NET Framework 4.7 开发 WinForm 应用程序。
我想打开一个 Web.config 文件,读取其 appSetting 部分并修改它。
要打开它,我使用这个:
 System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
它打开它,但是当我尝试使用以下方法获取密钥时:
string[] keys = config.AppSettings.Settings.AllKeys;
我得到一个空数组。
这是应用程序设置部分:
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" /> …