【问题标题】:How to resolve key/value pair with external .config file如何使用外部 .config 文件解析键/值对
【发布时间】:2020-08-12 05:36:58
【问题描述】:

我在 app.config 文件中指定 <appSettings>,我正在添加

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings configSource="ShareAppSettings.debug.config"/>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
    </startup>
</configuration>

ShareAppSettigns.debug.config 是我在本地计算机上使用的外部配置文件,我不想与团队的其他成员共享它。

ShareAppSettings.debug.config 看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
  <add key="clientID" value="11" />
  <add key="clientSecret" value="11" />
  <add key="tenantID" value="11" />
</appSettings>

每当我尝试调试主代码时:

private static List<string> AppCredentials()
{
   string clientID = ConfigurationManager.AppSettings["clientID"];
   string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
   string tenantID = ConfigurationManager.AppSettings["tenantID"];

   List<string> appCred = new List<string> { clientID, clientSecret, tenantID };

   if (clientID == null)
       throw new Exception("ShareAppSettings.Debug.Config file was not provided in this repo.");

   return (appCred);
}

由于某种原因,我没有获得 clientId、slientSecret 和tenantId 的值。此代码是 Grasshopper Add-on for v6 模板的一部分,它在 .NET Framework 4.7.1 上运行。每当我将相同的代码复制到同一框架的新 C# 控制台中时,都会构建代码。如果您能给我有关如何解决此问题的建议,我将不胜感激。

“EnableWindowsFormsHighDpiAutoResizing”是什么意思,如何使它工作?

非常感谢

enter image description here

【问题讨论】:

    标签: winforms app-config key-value-store grasshopper .net-4.7.1


    【解决方案1】:

    我建议使用 file 属性(对应于AppSettingsSection.File 属性)而不是 configSource 属性(对应于SectionInformation.ConfigSource 属性) 在 appSettings 部分。

    • configSource 不支持该部分中的其他键,而appSettings 可能包含应用程序所需的其他键,并且可能在其他地方(其他人可能会添加/删除它们 - 出于测试目的或任何其他原因)。

    file 属性允许在 appSettings 部分中存在其他键。

    您的app.config 文件可以是:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <appSettings file="ShareAppSettings.debug.config">
            <add key="DpiAwareness" value="PerMonitorV2"/>
        </appSettings>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
        </startup>
    </configuration>
    

    现在可以通过打开命名配置部分来访问这些值:

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
    
    var kvpClientID = appSettings.Settings["clientID"];
    var kvpClientSecret = appSettings.Settings["clientSecret"];
    var kvpCenantID = appSettings.Settings["tenantID"];
    
    string clientID = kvpClientID.Value;
    

    直接使用ConfigurationManager.AppSettings - 一个NameValueCollection - 返回指定键的值:

    string clientID = ConfigurationManager.AppSettings["clientID"];
    string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
    string tenantID = ConfigurationManager.AppSettings["tenantID"];
    

    请注意,使用file 属性,您的ShareAppSettings.debug.config 不需要(但不被禁止)XML 标头,它可以是:

    <appSettings>
      <add key="clientID" value="11" />
      <add key="clientSecret" value="11" />
      <add key="tenantID" value="11" />
    </appSettings>
    

    附注:
    您可以将file 属性设置为在运行时指向另一个文件并刷新appSettings 值以更新配置。
    请注意,如果已设置 file 属性,则该 .config 文件中包含的所有值都不会忽略,而是移至 [Application].exe.config 文件,成为 @987654346 的一部分@ 部分(因此,它们被保留)。
    使用file 属性的另一个原因可能更可取。

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var appSettings = config.AppSettings;
    appSettings.File = "SomeOtherFile.config";
    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("appSettings");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 2017-02-14
      • 2015-01-26
      • 2012-03-12
      相关资源
      最近更新 更多