您的位置:首页 > 编程语言 > C#

读取XML来更改ipconfig的配置

2016-04-07 21:27 651 查看
```
XmlDocument doc=new XmlDocument();  
//获得配置文件的全路径  

/*下面的代码是读取bin目录下debug里面的配置文件,他与读取当前项目目录下ipconfig里面的配置文件的区别,可以查找相关文献,下面只提供代码*/
string strFileName = AppDomain.CurrentDomain.BaseDirectory.ToString();
strFileName = strFileName+ "PMSClient.EXE.Config";

//下面的代码是读取当前项目目录下ipconfig里面的配置文件,
//DirectoryInfo dir = new DirectoryInfo(Application.StartupPath).Parent.Parent;
//string strFileName = dir.FullName;
//strFileName = strFileName+ "\\app.config";

try
{

doc.Load(strFileName);
//找出名称为“add”的所有元素  
XmlNodeList nodes = doc.GetElementsByTagName("add");
for (int i = 0; i < nodes.Count; i++)
{
//获得将当前元素的key属性    
XmlAttribute att = nodes[i].Attributes["key"];
//根据元素的第一个属性来判断当前的元素是不是目标元素    
if (att.Value == "ComputerID")
{
//对目标元素中的第二个属性赋值     
att = nodes[i].Attributes["value"];
att.Value = ComputerNO_temp;
break;
}
}
//保存上面的修改  
doc.Save(strFileName);
}
catch { }


“`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息