2012-05-26 45 views
110

我無法訪問配置文件中的值。AppSettings從.config文件獲取值

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> 

你有什麼建議,我該怎麼辦?

回答

230

這個工作對我來說:

string value = System.Configuration.ConfigurationManager.AppSettings[key]; 
+11

我試過這個方法,但是我得到了空字符串。 – Matt

+1

請參閱:http://stackoverflow.com/a/10718733/730701 – Adam

+0

我不明白。你認爲我沒有訪問配置文件嗎? – Matt

18

這給一展身手:

string filePath = ConfigurationManager.AppSettings["ClientsFilePath"]; 
+2

這個方法我試過,但我得到了空字符串。 – Matt

+4

您的實施必須是不正確的。這絕對適用於從配置文件中檢索關鍵值。 – dtsg

3

我使用:

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"]; 

其中值1 = ...值3 = ...給出null和值2 = ...工作

然後,我決定更換內部的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

0

我簡單的測試也失敗了,以下的建議另一個答案在這裏 - 直到我意識到我添加到我的桌面應用程序的配置文件被命名爲「App1.config」。我將它重命名爲「App.config」,並且一切都立即按照它應該的方式工作。

33

是dtsg給作品答案:

string filePath = ConfigurationManager.AppSettings["ClientsFilePath"]; 

,但你需要一個集引用

System.Configuration

轉到添加到您的解決方案資源管理器右鍵點擊上的參考文獻和選擇添加參考文獻。選擇組件選項卡並搜索配置

Reference manager

這裏是我的應用的一個例子。配置

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <appSettings> 
    <add key="AdminName" value="My Name"/> 
    <add key="AdminEMail" value="MyEMailAddress"/> 
    </appSettings> 
</configuration> 

,您可以通過以下方式獲得:

string adminName = ConfigurationManager.AppSettings["AdminName"]; 
+2

我似乎有與OP相同的問題。使用VS2015。我不知道如何或爲什麼,但是我對System.Configuration的引用說'System.configuration'(小寫)。 – Tim

1

見我做了我認爲是很明顯的事情是:

string filePath = ConfigurationManager.AppSettings.GetValues("ClientsFilePath").ToString(); 

雖然總是對它進行編譯返回null。

然而,這(從上面)工作原理:

string filePath = ConfigurationManager.AppSettings["ClientsFilePath"]; 
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將Env的值ironment鍵+ 「_Emails」

example : [email protected];[email protected]; 
10

閱讀從配置:

你需要參考您的項目

  • 轉到「添加到配置

    1. 打開 」屬性「設置「標籤
    2. 添加」名稱「和」值「
    3. 使用以下代碼獲取值:

      string value = Properties.Settings.Default.keyname;

    保存到配置:

    Properties.Settings.Default.keyName = value; 
        Properties.Settings.Default.Save(); 
    
  • +1

    如上所述,如果您在項目屬性的「設置」選項卡中添加值,那麼使用ConfigurationManager的所有其他答案都不起作用,這非常有幫助。謝謝。 –

    +0

    這也適用於我。我得到了一個帶有上述建議的'null'字符串,但是'Properties.Settings.Default。 '像一個魅力工作! –

    2

    在應用程序/網絡config文件設置以下配置:

    <configuration> 
        <appSettings> 
        <add key="NameForTheKey" value="ValueForThisKey" /> 
        ... 
        ...  
        </appSettings> 
    ... 
    ... 
    </configuration> 
    

    ,那麼你可以通過將這個訪問這個在你的代碼line:

    string myVar = System.Configuration.ConfigurationManager.AppSettings["NameForTheKey"]; 
    

    *注意t這對於.net4.5.x和.net4.6.x的工作很好。但不適用於.net核心。 此致敬禮: 拉斐爾

    2

    對於Web應用程序,我通常會寫這個方法,只是調用它的關鍵。

    private String GetConfigValue(String key) 
        { 
         return System.Web.Configuration.WebConfigurationManager.AppSettings[key].ToString(); 
        } 
    
    0

    您可以簡單地輸入:

    string filePath = Sysem.Configuration.ConfigurationManager.AppSettings[key.ToString()]; 
    

    因爲key是一個對象,AppSettings需要一個字符串