Dot-Net
如何編輯應用程序配置設置?App.config 最好的方法是什麼?
對於我的項目,我有通過項目屬性中的設置添加的設置。
我很快發現直接編輯 app.config 文件似乎並沒有真正更新設置值。似乎我必須在進行更改然後重新編譯時查看項目屬性。
- 我想知道……為項目處理可定制設置的最佳和最簡單的方法是什麼 - 認為這對於.Net如何處理它來說是****不費吹灰之力的……真讓我感到羞恥。
- 是否可以使用其中一種設置AppSettings、 ApplicationSettings或UserSettings來處理這個問題?
將我的設置寫入自定義配置文件並自己處理會更好嗎?
現在…我正在尋找最快的解決方案!
我的環境是 C#、.Net 3.5 和 Visual Studio 2008。
更新
我正在嘗試執行以下操作:
protected override void Save() { Properties.Settings.Default.EmailDisplayName = this.ddEmailDisplayName.Text; Properties.Settings.Default.Save(); }編譯時給我一個只讀錯誤。
這很愚蠢……我想我必須為浪費大家的時間而道歉!但看起來我只需要將範圍設置為使用者而不是應用程序,我就可以編寫新值。
我也試圖解決這個需求,我現在有一個漂亮漂亮的 ConsoleApplication,我想分享它:(App.config)
你會看到的是:
- 如何讀取所有 AppSetting 屬性
- 如何插入新屬性
- 如何刪除屬性
- 如何更新屬性
玩得開心!
public void UpdateProperty(string key, string value) { Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); KeyValueConfigurationCollection appSettings = config.AppSettings.Settings; // update SaveBeforeExit config.AppSettings.Settings[key].Value = value; Console.Write("...Configuration updated: key "+key+", value: "+value+"..."); //save the file config.Save(ConfigurationSaveMode.Modified); Console.Write("...saved Configuration..."); //relaod the section you modified ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name); Console.Write("...Configuration Section refreshed..."); } public void ReadAppSettingsProperty() { try { var section = ConfigurationManager.GetSection("applicationSettings"); // Get the AppSettings section. NameValueCollection appSettings = ConfigurationManager.AppSettings; // Get the AppSettings section elements. Console.WriteLine(); Console.WriteLine("Using AppSettings property."); Console.WriteLine("Application settings:"); if (appSettings.Count == 0) { Console.WriteLine("[ReadAppSettings: {0}]", "AppSettings is empty Use GetSection command first."); } for (int i = 0; i < appSettings.Count; i++) { Console.WriteLine("#{0} Key: {1} Value: {2}", i, appSettings.GetKey(i), appSettings[i]); } } catch (ConfigurationErrorsException e) { Console.WriteLine("[ReadAppSettings: {0}]", e.ToString()); } } public void updateAppSettingProperty(string key, string value) { // Get the application configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); string sectionName = "appSettings"; config.AppSettings.Settings.Remove(key); config.AppSettings.Settings.Add(key, value); SaveConfigFile(config); } public void insertAppSettingProperty(string key, string value) { // Get the application configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); string sectionName = "appSettings"; config.AppSettings.Settings.Add(key, value); SaveConfigFile(config); } public void deleteAppSettingProperty(string key) { // Get the application configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings.Remove(key); SaveConfigFile(config); } private static void SaveConfigFile(System.Configuration.Configuration config) { string sectionName = "appSettings"; // Save the configuration file. config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section. This // makes the new values available for reading. ConfigurationManager.RefreshSection(sectionName); // Get the AppSettings section. AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection(sectionName); Console.WriteLine(); Console.WriteLine("Using GetSection(string)."); Console.WriteLine("AppSettings section:"); Console.WriteLine(appSettingSection.SectionInformation.GetRawXml()); } }配置文件如下所示:
<configuration> <configSections> </configSections> <appSettings> <add key="aNewKey1" value="aNewValue1" /> </appSettings>好吧,所以我對這個解決方案的 AppSettings 沒有任何問題!玩得開心… ;-) !