2011-06-01 58 views
0

我知道:Visual Studio 2008應用程序設置未保存?

應用程序設置可以被存儲爲XML是可序列化的或具有實現的ToString/FromString的TypeConverter,任何數據類型。最常見的類型是String,Integer和Boolean,但您也可以將值存儲爲Color,Object或連接字符串。

我有一個ListDictionary類設置 - 這是序列化的,但每次我重新啓動我的應用程序爲null,儘管創建,分配和保存ListDictionary我的設置:

Properties.Settings.Default.Preferences = new System.Collections.Specialized.ListDictionary(); 
Properties.Settings.Default.Preferences.Add(1, "test"); 
Properties.Settings.Default.Save(); 

如何使用像應用程序設置這樣的類?我需要一個詞典類型集合作爲設置...

回答

1

有一套我希望你嘗試的東西。

  1. 確保您創建的設置範圍爲USER。

http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx 添加2個字符串(DictionaryKey和Dictionaryvalue)到設置,並設置的範圍爲用戶和值作爲空白

  1. 設置不包含選項添加字典。所以,我的建議是嘗試這個代碼

化妝2刺痛設置,並做到這一點

Properties.Settings.Default.Dictionarykey = "";// To empty previous settings 
Properties.Settings.Default.Dictionaryvalue = "";// To empty previous settings 
for (int i = 0; i < yourdictionarycount; i++) 
{ 
Properties.Settings.Default.Dictionarykey += i + "#"; // or any other value     
Properties.Settings.Default.Dictionaryvalue += "test" + "#"; // or any other value     
} 
Properties.Settings.Default.Save(); 

而且當你正在檢索值

使用此代碼:

public Dictionary<int, string> savedsettings = new Dictionary<int, string>(); 

string[] eachkey = Properties.Settings.Default.Dictionarykey.Split('#'); 
string[] value = Properties.Settings.Default.Dictionaryvalue.Split('#'); 

for (int j = 0; j < eachkey.Length; j++) 
{ 
    savedsettings.Add(eachkey[i], eachvalue[i]); 
} 
//just to check if the values are being retrieved properly 
Messagebox.Show(Properties.Settings.Default.Dictionarykey); 

Regards,

Vivek Sampara

+0

是的,它是用戶。 「設置不包含添加Dictionar的選項」 - 但我可以通過瀏覽到System.Collections.Specialized命名空間來添加ListDictionary?儘管如此,您添加兩個字符串的想法(我認爲我會使用StringCollections的工作方式)將會有所斬獲。謝謝! – Lara 2011-06-01 11:09:08

+0

沒問題勞拉,祝你有美好的一天:) – 2011-06-01 21:46:22

相關問題