2017-01-16 26 views
0

我已經定義:加載數據,並連接到屬性

public static string Firstname { get; set; } 
public static string Surname { get; set; } 
public static bool DocBook5 { get; set; } 
public static string Language { get; set; } 

我存儲在App.config該信息。

然後我用的是裝載並連接:

Firstname = ConfigurationManager.AppSettings["firstname"]; 
Surname = ConfigurationManager.AppSettings["surname"]; 
DocBook5 = Convert.ToBoolean(ConfigurationManager.AppSettings["docbook5"]); 
Language = ConfigurationManager.AppSettings["language"]; 

但是,如果我只是給出一些屬性內容爲Console.WriteLine,它看起來像屬性爲空。 GetConfig類的完整代碼可以顯示there

也許有人知道爲什麼?

+0

您可以分享您的app.config嗎?你的項目是一個控制檯應用程序或網絡? –

+0

也可以告訴我爲什麼你需要靜態屬性? –

+0

您的密鑰位於下的節點不在AppSetting下。並且您的代碼正在從appsetting部分讀取。所以你得到空白結果 –

回答

0

看看你的App.config。 您存儲的數據是用戶設置而不是appSettings,但是您從appSettings獲取了數據。

要麼你把所有的appSettings您的數據,或者你定義configSections,在其中添加名稱UserSettings。 (使用「GetSection」而不是「 的AppSettings」)

後來,在代碼中,你需要申請的特定部分,並從那裏得到的數據。

編輯:讓我知道如果你想要的代碼示例,以滿足您的app.config文件

+0

嗨,Ori,你能提供一個代碼示例嗎?我嘗試了這樣:ConfigurationManager.GetSection(「firstname」)。ToString();但那不起作用。 –

+0

您想獲得'UserSettings'部分(使用它) –

+0

是的。這將是偉大的:-) –