標籤:
//讀取到這個節點
string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(file); string comNumber = config.AppSettings.Settings["comNumber"].Value.ToString(); comboBox1.SelectedItem = comNumber; //設定這個節點方法1,親測可行 string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(file); bool exist = false;//記錄這個com連接埠值是否存在 if (config.AppSettings.Settings["comNumber"] != null) { exist = true; } if (exist) { config.AppSettings.Settings.Remove("comNumber"); } config.AppSettings.Settings.Add("comNumber", comNO); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings");
//方法2,因為上方法可行,這個並沒有測試 //Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //config.AppSettings.Settings. //config.AppSettings.Settings.Add("comNumber", comNO); //config.Save(ConfigurationSaveMode.Full); //ConfigurationManager.RefreshSection("appSettings");
匯入的是一個System.Configuration的,
同樣的connectionString也是差不多的,這裡沒有摘錄,網上都是
關於winform中*.exe.config中的appSettings的節點的讀取與修改