決定VS2005配置的設(shè)置問題和方式
希望我對VS2005配置的一點經(jīng)驗?zāi)芙o大家?guī)韼椭@里將介紹VS2005配置問題和一些解決方法,在這里拿出來和大家分享一下。希望以下的文章對大家有良好的交流平臺。。。
例如:DateTime userDateTime1 = Properties.Settings.Default.userDateTime1; 真是方便了很多。但是你有沒有發(fā)現(xiàn),使用 Properties.Settings.Default.Save() 保存了設(shè)置后,Application 范圍的設(shè)置為什么沒有保存成功,User 范圍的設(shè)置的變化為什么沒有體現(xiàn)到 app.config 文件中去呢?
1. 在VS 2005中進行應(yīng)用程序設(shè)置打開 項目屬性 » 設(shè)置,如下圖:
輸入名稱,選擇類型和范圍,輸入值保存即完成設(shè)置。類型:int,string,DateTime等各種數(shù)據(jù)類型;范圍:Application 范圍的設(shè)置對所有用戶都有效;
User 范圍的設(shè)置對當前用戶(當前 Windows 登錄的用戶)有效,同一個設(shè)置每個用戶可以有不同的值,而且互不影響。
2. 讀取配置文件(讀取應(yīng)用程序設(shè)置)
無論是Application 范圍的設(shè)置,還是User 范圍的設(shè)置,讀取的方法都是一樣的。
- 讀取設(shè)置
- this.appSetting1TextBox.Text = Properties.Settings.Default.appSetting1;
- this.userSetting1TextBox.Text = Properties.Settings.Default.userSetting1;
3. 保存 User 范圍配置文件(保存 User 范圍的應(yīng)用程序設(shè)置)
- 保存 User 范圍的設(shè)置
- Properties.Settings.Default.userSetting1 = this.userSetting1TextBox.Text;
- Properties.Settings.Default.Save();
VS2005配置文件沒有保存在應(yīng)用程序文件夾下,而是保存在這里:X:\Documents and Settings\Windows登錄用戶\Local Settings\Application Data。
4. 保存 Application 范圍配置文件(保存 Application 范圍的應(yīng)用程序設(shè)置) 保存 Application 范圍配置文件可沒有保存 User 范圍配置文件那樣簡單,直接 Properties.Settings.Default.Save() 是不行的。因為 Application 范圍的設(shè)置在運行時是“只讀”的。這里使用的方法是使用 XmlDocument 來直接保存 config 文件,然后在 Reload 設(shè)置。
- 保存 Applicationi 范圍的設(shè)置
- string configFileName = Application.ExecutablePath + ".config";
- System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
- doc.Load(configFileName);
- string configString = @"configuration/applicationSettings/SetConfig.Properties.Settings/setting[@name='appSetting1']/value";
- System.Xml.XmlNode configNode = doc.SelectSingleNode(configString);
- if (configNode != null)
- {
- configNode.InnerText = this.appSetting1TextBox.Text;
- doc.Save(configFileName);
- // 刷新應(yīng)用程序設(shè)置,這樣下次讀取時才能讀到***的值。
- }
順便說一下:使用 Properties.Settings.Default.Reset() 可以恢復(fù)VS2005配置設(shè)置的默認值(從 app.config 中恢復(fù))。
【編輯推薦】