2

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

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

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

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

我究竟做错了什么?

4

3 回答 3

3

WebConfigurationManager.OpenWebConfiguration包括以下path参数说明:

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

因为您的应用程序没有在 IIS 下作为网站运行,所以Web.config打开的应用程序实际上是位于 .NET Framework 安装文件夹本身中的应用程序(在我的情况下,就是 .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)。

于 2018-08-30T09:25:39.653 回答
0

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

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

var keys = ConfigurationManager.AppSettings.AllKeys
于 2018-08-30T09:31:18.943 回答
0

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

试试这样:

config = WebConfigurationManager.OpenWebConfiguration("~");

这也可能对您有所帮助:您如何在运行时修改 web.config appSettings?

于 2018-08-30T09:11:20.020 回答