如何修改app.config 文件呢,用ConfigurationSettings.AppSettings.Set 方式程序會報錯,在網(wǎng)上找到一段動態(tài)修改winform app.config的代碼,大家分享一下:
public static void UpdateConfig(string p_strKey, string p_strValue) { try { string m_strFullPath = ""; // Assembly Asm = Assembly.GetExecutingAssembly(); XmlDocument xmlDoc =new XmlDocument(); m_strFullPath = System.Windows.Forms.Application.ExecutablePath + ".config"; xmlDoc.Load(m_strFullPath); XmlNodeList nodeList=xmlDoc.SelectSingleNode("/configuration/appSettings").ChildNodes; foreach(XmlNode xn in nodeList { XmlElement xe=(XmlElement)xn;
if( xe.GetAttribute("key").IndexOf(p_strKey) != -1 ) { xe.SetAttribute("value",p_strValue); } } xmlDoc.Save(m_strFullPath); } catch(System.NullReferenceException NullEx) { throw NullEx; } catch(Exception ex) { throw ex; } }
|