【问题标题】:Read appSettings section from Web.config file using WebConfigurationManager使用 WebConfigurationManager 从 Web.config 文件中读取 appSettings 部分
【发布时间】:2018-08-30 17:31:18
【问题描述】:

我正在使用 C# 和 .NET Framework 4.7 开发一个 WinForm 应用程序。

我想打开一个 Web.config 文件,阅读它的 appSetting 部分并修改它。

打开它,我用这个:

 System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(null);

它会打开它,但是当我尝试使用以下方法获取密钥时:

string[] keys = config.AppSettings.Settings.AllKeys;

我得到一个空数组。

这是 appSetting 部分:

<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" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    <add key="MinRemainingCodes" value="100" />
    <!-- Others keys -->
  </appSettings>

</configuration>

也许问题是它没有打开文件,而是在documentation说:

配置文件的虚拟路径。如果为空,则根 Web.config 文件已打开。

也许我不明白root 是什么意思,因为程序和Web.config 文件在同一个文件夹中。

我做错了什么?

【问题讨论】:

  • 下面的例子对你有用吗? WebConfigurationManager.AppSettings
  • 试试string[] keys =ConfigurationManager.AppSettings.AllKeys?
  • @DanielShillcock 谢谢,但它不起作用。我已经用更多细节更新了我的问题。

标签: c# appsettings configurationmanager webconfigurationmanager


【解决方案1】:

WebConfigurationManager.OpenWebConfiguration包含path参数的如下描述:

配置文件的虚拟路径。如果为 null,则打开根 Web.config 文件。

因为您的应用程序没有在 IIS 下作为网站运行,所以正在打开的 Web.config 实际上是位于 .NET Framework 安装文件夹本身中的那个(在我的例子中,它是 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config)。

WebConfigurationManager.OpenMappedWebConfiguration 允许您将虚拟目录映射到物理目录,以便您指定映射到您自己的本地目录的虚拟路径。这是我用来完成这项工作的代码:

var webConfigurationFileMap = new WebConfigurationFileMap();

webConfigurationFileMap.VirtualDirectories.Add(
    string.Empty,
    new VirtualDirectoryMapping(Directory.GetCurrentDirectory(), isAppRoot: true));

var webConfig = WebConfigurationManager.OpenMappedWebConfiguration(
    webConfigurationFileMap,
    string.Empty);

如您所见,我将根虚拟目录(使用string.Empty)映射到应用程序的目录(使用Directory.GetCurrentDirectory)。

【讨论】:

    【解决方案2】:

    如果我没记错的话,OpenWebConfiguration 应该会接收到你的网络配置的路径,而你正在传递它null

    试试这样:

    config = WebConfigurationManager.OpenWebConfiguration("~");
    

    这也可能对您有所帮助:How do you modify the web.config appSettings at runtime?

    【讨论】:

    • 谢谢,但出现以下异常:“此处不允许应用程序的虚拟相对路径 '~'。”
    • 您是否尝试过读取特定值?为了确保您能够实际打开带有 null 的 Web 配置。类似于:config.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text。如果可行,请尝试“Request.ApplicationPath”而不是“~”或 null
    • 我正在开发一个 WinForm 应用程序。我是从那个应用程序打开它,而不是从 ASP.NET 应用程序。
    【解决方案3】:

    首先,您将 web.config 用于桌面应用程序。听起来不正确。尝试改用 app.config。 二、WebConfigurationManager.OpenWebConfiguration 打开Web-application配置文件

    关于主题,从配置文件中获取信息尝试使用

    var keys = ConfigurationManager.AppSettings.AllKeys
    

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 2017-02-17
      • 1970-01-01
      • 2013-05-09
      • 2016-01-11
      • 2015-05-11
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多