【问题标题】:How to use custom configuration file or app.config in .NET application如何在 .NET 应用程序中使用自定义配置文件或 app.config
【发布时间】:2016-04-28 19:49:14
【问题描述】:

我有 MVC5 .NET 4.6.1 C# Web 应用程序 我想创建一个独立于 web.config 的自定义配置文件来存储我的应用程序使用的一些设置。

我尝试关注这篇文章https://support.microsoft.com/en-us/kb/815786

但是我在 app.config 中设置的项目:

<?xml version="1.0"?>
<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.6.1" />
      <httpRuntime targetFramework="4.6.1" />
    </system.web>
  <appSettings>
      <add key="Key0" value="0" />
      <add key="Key1" value="1" />
      <add key="Key2" value="2" />
   </appSettings>

</configuration>

在我的应用程序中看不到,例如。它们为空:

string attr = ConfigurationManager.AppSettings["Key0"];

为什么它不起作用?我错过了什么吗?

或者,我想创建一个自定义配置文件,例如。 mycustom.config 来定义我的全局应用设置。

编辑

我使用的解决方案

关注这个帖子https://social.msdn.microsoft.com/Forums/vstudio/en-US/11e6d326-c32c-46b1-a9a2-1fbef96f33ee/howto-custom-configuration-files?forum=netfxbcl

在 web.config 中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
       <configSections>
              <section name="newAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
       </configSections>
       <newAppSettings file="C:\mycustom.config"/>
</configuration>

然后是 mycustom.config

<?xml version="1.0" encoding="utf-8" ?>
<newAppSettings>
       <add key="OurKey" value="OurValue"/>
</newAppSettings>

并读取值:

System.Collections.Specialized.NameValueCollection newAppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("newAppSettings");
string key = Convert.ToDateTime(newAppSettings["OurKey"]);

【问题讨论】:

    标签: c# .net configuration app-config configuration-files


    【解决方案1】:

    您可以为连接字符串和应用设置使用单独的配置文件:

    <appSettings configSource="appSettings.config" />
    <connectionStrings configSource="connectionStrings.config"/>
    

    appSettings.config 文件

    <?xml version="1.0" encoding="utf-8" ?>
    <appSettings>
        <add key="Setting1" value="App setting 1" />
    </appSettings>
    

    connectionStrings.config 文件

    <?xml version="1.0" encoding="utf-8"?>
    <connectionStrings>
        <add name="MyConnStr1" connectionString="My connection string" />
    </connectionStrings>
    

    用法和以前一样:

    var setting1 = ConfigurationManager.AppSettings["Setting1"];
    var connString1 = ConfigurationManager.ConnectionStrings["MyConnStr1"].ConnectionString;
    

    【讨论】:

    • 不完全是我想要的,但也很有用,但我没有尝试它是否有效 - 我认为它确实有效。我使用了稍微不同的方法,这对我的应用程序更方便/个性化 - 请参阅我的编辑。感谢您的帮助
    【解决方案2】:
                //Helps to open the Root level web.config file.
                Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
    
                //Modifying the AppKey from AppValue to AppValue1
                webConfigApp.AppSettings.Settings["AppKey"].Value = "AppValue1";
    
    
    
    <appSettings>
        <add key="AppKey" value="AppValue"/>
      </appSettings>
    

    【讨论】:

    • 我在读取 web.config 时没有问题,我在读取 app.config 文件时遇到问题...
    • 我没有尝试过它,但它似乎是我正在寻找的解决方案,但是我使用了另一种方法,例如。通过基于此social.msdn.microsoft.com/Forums/vstudio/en-US/… 使用自定义应用设置部分创建自定义配置文件
    猜你喜欢
    • 2018-10-01
    • 2013-05-06
    • 2014-05-30
    • 2013-04-23
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多