修改app文件
//修改app
SetConfigValue("..\\..\\App.config", "appSettings", mfserver);
//读取并修改app.config文件
public void SetConfigValue(string path, string appKey, string Context)
{
XmlDocument xDoc = new XmlDocument();
string[] mfArr = Context.Split(',');
try
{
xDoc.Load(path);
XmlNodeList appsetting = xDoc.GetElementsByTagName(appKey);
XmlNodeList xnl = appsetting[0].ChildNodes;
XmlNode xNode = xDoc.SelectSingleNode("//appSettings");
bool flagIP = false;
bool flagPort = false;
for (int i = 0; i < xnl.Count; i++)
{
if (xnl[i].Attributes["key"].Value == "MFServerIP")
{
xnl[i].Attributes["value"].Value = mfArr[0];
flagIP = true;
}
if (xnl[i].Attributes["key"].Value == "MFServerPort")
{
xnl[i].Attributes["value"].Value = mfArr[1];
flagPort = true;
}
}
//没有MFServerIP,MFServerPort节点则添加
if (flagIP == false)
{
XmlElement xElem = xDoc.CreateElement("add");
xElem.SetAttribute("key", "MFServerIP");
xElem.SetAttribute("value", mfArr[0]);
xNode.AppendChild(xElem);
}
if (flagPort == false)
{
XmlElement xElem = xDoc.CreateElement("add");
xElem.SetAttribute("key", "MFServerPort");
xElem.SetAttribute("value", mfArr[1]);
xNode.AppendChild(xElem);
}
s xDoc.Save(path);
MessageBox.Show("Configure Successfully");
}
catch (Exception)
{
MessageBox.Show("Configure Failed");
}
}