195

構成ファイルの値にアクセスできません。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var clientsFilePath = config.AppSettings.Settings["ClientsFilePath"].Value; 
// the second line gets a NullReferenceException

.config ファイル:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <!-- ... -->
    <add key="ClientsFilePath" value="filepath"/>
    <!-- ... -->
  </appSettings>
</configuration>

私が何をすべきか提案はありますか?

4

16 に答える 16

395

これは私のために働く:

string value = System.Configuration.ConfigurationManager.AppSettings[key];
于 2012-05-26T13:30:49.960 に答える
21

これを試してください:

string filePath = ConfigurationManager.AppSettings["ClientsFilePath"];
于 2012-05-26T13:28:34.427 に答える
8

私は使っている:

    ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
    //configMap.ExeConfigFilename = @"d:\test\justAConfigFile.config.whateverYouLikeExtension";
    configMap.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + ServiceConstants.FILE_SETTING;
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
    value1 = System.Configuration.ConfigurationManager.AppSettings["NewKey0"];
    value2 = config.AppSettings.Settings["NewKey0"].Value;
    value3 = ConfigurationManager.AppSettings["NewKey0"];

value1 = ...およびvalue3 = ...が null を返し、 value2 = ...が機能する場所

次に、内部の app.config を次のように置き換えることにしました。

// Note works in service but not in wpf
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"d:\test\justAConfigFile.config.whateverYouLikeExtension");
ConfigurationManager.RefreshSection("appSettings");

string value = ConfigurationManager.AppSettings["NewKey0"];

VS2012 .net 4 の使用

于 2014-04-30T06:54:37.540 に答える
2

回答のいくつかは、 IMOから少しずれているようです。これが2016 年頃 の私の見解です。

<add key="ClientsFilePath" value="filepath"/>

参照されていることを確認しSystem.Configurationてください。

質問はappsettingsキーの値を求めています

最も確実にすべきこと

  string yourKeyValue = ConfigurationManager.AppSettings["ClientsFilePath"]

  //yourKeyValue should hold on the HEAP  "filepath"

これは、値をグループ化できるひねりです(この質問ではありません)

var emails = ConfigurationManager.AppSettings[ConfigurationManager.AppSettings["Environment"] + "_Emails"];

emails環境キー + "_Emails" の値になります

example :   jack@google.com;thad@google.com;
于 2016-06-02T21:42:23.720 に答える
2

Web アプリケーションの場合、通常はこのメソッドを記述し、キーで呼び出すだけです。

private String GetConfigValue(String key)
    {
       return System.Web.Configuration.WebConfigurationManager.AppSettings[key].ToString();
    }
于 2017-05-15T08:30:29.303 に答える
1
  1. プロジェクトの「プロパティ」を開きます
  2. 「設定」タブに移動します
  3. 「名前」と「値」を追加

コードは自動的に生成されます

    <configuration>
      <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup ..." ... >
          <section name="XX....Properties.Settings" type="System.Configuration.ClientSettingsSection ..." ... />
        </sectionGroup>
      </configSections>
      <applicationSettings>
        <XX....Properties.Settings>
          <setting name="name" serializeAs="String">
            <value>value</value>
          </setting>
          <setting name="name2" serializeAs="String">
            <value>value2</value>
          </setting>
       </XX....Properties.Settings>
      </applicationSettings>
    </configuration>

値を取得するには

Properties.Settings.Default.Name

また

Properties.Settings.Default["名前"]

于 2019-04-09T13:00:18.910 に答える
0

デスクトップアプリケーションに追加した構成ファイルに「App1.config」という名前が付けられていることに気付くまで、ここの他の回答のアドバイスに従って、私の簡単なテストも失敗しました。名前を「App.config」に変更すると、すべてがすぐに正常に機能しました。

于 2014-08-29T19:11:52.650 に答える