XmlDocument doc = new XmlDocument();
doc.Load(Application.ExecutablePath + ".config");
XmlNode node = doc.SelectSingleNode("//add[@key='ServerName']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value", "");
doc.Save(Application.ExecutablePath + ".config");
红色部分的路径在DEBUG下,可我的CONFIG文件就是在工程下创建的,我也没打包试过,我就是希望程序打包后,在设置服务器信息的时候,对设置好的信息后可以正常保存到该保存的CONFIG文件里
蓝部分提示未将对象引用设置到对象的实例。
请教下这里问题该怎么解决,谢谢。
doc.Load(Application.ExecutablePath + ".config");
XmlNode node = doc.SelectSingleNode("//add[@key='ServerName']");
XmlElement ele = (XmlElement)node;
ele.SetAttribute("value", "");
doc.Save(Application.ExecutablePath + ".config");
红色部分的路径在DEBUG下,可我的CONFIG文件就是在工程下创建的,我也没打包试过,我就是希望程序打包后,在设置服务器信息的时候,对设置好的信息后可以正常保存到该保存的CONFIG文件里
蓝部分提示未将对象引用设置到对象的实例。
请教下这里问题该怎么解决,谢谢。
那就去实例呀。。!
你不知道new 一个吗?
private static bool UpdateConfig(string strKey, string strValue)
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(Application.ExecutablePath + ".config");
XmlNode node = doc.SelectSingleNode(@"//add[@key='" + strKey + "']");
XmlElement ele = (XmlElement)node;
if (ele != null)
{
ele.SetAttribute("value", strValue);
doc.Save(Application.ExecutablePath + ".config");
}
}
catch
{
return false;
}
return true;
}
Configuration C=ConfigurationManager.OpenExeConfiguration(String .Empty);
C.AppSettings.Settings["Server"].Value = "";
C.Save(ConfigurationSaveMode.Modified);
public void SaveConfig(string configKey,string configValue)
{
XmlDocument doc=new XmlDocument();
doc.Load(strFileName);
XmlNodeList nodes=doc.GetElementsByTagName("add");
foreach (XmlNode node in nodes)
{
XmlElement xe = (XmlElement)node;
if (xe.GetAttribute("key") == configKey)
{
xe.SetAttribute("value", configValue);
}
}
doc.Save(strFileName);
}