使用WebConfigurationManager从Web.config文件中读取appSettings部分

问题描述 投票:1回答:3

我正在使用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说:

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

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

我究竟做错了什么?

c# appsettings configurationmanager webconfigurationmanager
3个回答
2
投票

WebConfigurationManager.OpenWebConfiguration包括以下对path参数的描述:

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

因为您的应用程序不是作为Web站点在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)。


0
投票

OpenWebConfiguration应该接收你的web配置的路径,如果我没有弄错,你传递它null

试试这样:

config = WebConfigurationManager.OpenWebConfiguration("~");

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


0
投票

首先,您将web.config用于桌面应用程序。这听起来不正确。尝试使用app.config。其次,WebConfigurationManager.OpenWebConfiguration打开Web应用程序配置文件

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

var keys = ConfigurationManager.AppSettings.AllKeys
© www.soinside.com 2019 - 2024. All rights reserved.