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

3

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

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

为了打开它,我使用以下代码:

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

它打开了它,但是当我尝试使用以下代码获取密钥时:

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

我可以帮助您翻译这段内容。该段内容与IT技术相关。

我得到了一个空数组。

这是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>

也许问题在于它没有打开文件,但在文档中说:

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

也许我不明白 root 的含义,因为程序和 Web.config 文件在同一个文件夹中。
我做错了什么?

以下示例对您是否有效?WebConfigurationManager.AppSettings - IronAces
尝试使用string[] keys = ConfigurationManager.AppSettings.AllKeys - A_Sk
@DanielShillcock 谢谢,但是还是不行。我已经更新了我的问题并提供了更多细节。 - VansFannel
3个回答

5

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 config路径,如果我没记错的话,而您传递了null

试试这样:

config = WebConfigurationManager.OpenWebConfiguration("~");

还有这个可能会对你有帮助:如何在运行时修改web.config的appSettings?


谢谢,但是我遇到了以下异常:"应用程序的虚拟相对路径'~'在此处不允许使用。" - VansFannel
你尝试过读取特定的值吗?为了确保你能够打开带有null的web config。类似这样:config.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text。如果这样可以的话,尝试使用"Request.ApplicationPath"代替'~'或null。 - João Cardoso
我正在开发一个WinForm应用程序。我是从那个应用程序中打开它,而不是从ASP.NET应用程序中打开。 - VansFannel

0
首先,你在桌面应用程序中使用web.config是不正确的。尝试使用app.config代替。 其次,WebConfigurationManager.OpenWebConfiguration打开Web应用程序配置文件。
至于获取配置文件信息的话题,请尝试使用
var keys = ConfigurationManager.AppSettings.AllKeys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接