1 /// <summary> 2 /// 修改WCF配置文件 3 /// </summary> 4 public class ConfigWcfEndpoing 5 { 6 /// <summary> 7 /// 修改WCF 客户端服务IP地址修改 8 /// </summary> 9 /// <param name="serverip">服务器的IP地址</param> 10 public void ChangeWCF(string serverip) 11 { 12 14 var xmlDocument = new XmlDocument(); 15 xmlDocument.Load("SIMU-Config.exe.config"); 16 XmlNode singleNode = xmlDocument.SelectSingleNode("configuration/system.serviceModel/client"); 17 if (singleNode != null) 18 { 19 const string pattern = "://.*"; 20 XmlNodeList servernodelist = singleNode.ChildNodes; 21 foreach (XmlNode node in servernodelist) 22 { 23 if (node.Attributes != null) 24 { 25 string address = node.Attributes["address"].Value; 26 switch (node.Attributes["name"].Value) 27 { 28 case "endpointBStationAlertData": 29 string replacement1 = string.Format("://{0}:{1}/ServiceBStationAlertData/tcp", serverip, "5118"); 30 address = Regex.Replace(address, pattern, replacement1); 31 if (node.Attributes != null) 32 { 33 node.Attributes["address"].InnerText = address; 34 } 35 break; 36 case "endpointPersonLocData": 37 string replacement2 = string.Format("://{0}:{1}/ServicePersonLocData/tcp", serverip, "1118"); 38 address = Regex.Replace(address, pattern, replacement2); 39 if (node.Attributes != null) 40 { 41 node.Attributes["address"].InnerText = address; 42 } 43 break; 44 45 case "endpointPersonAlertData": 46 string replacement3 = string.Format("://{0}:{1}/ServicePersonAlertData/tcp", serverip, "2118"); 47 address = Regex.Replace(address, pattern, replacement3); 48 if (node.Attributes != null) 49 { 50 node.Attributes["address"].InnerText = address; 51 } 52 break; 53 54 case "endpointGeneralQuery": 55 string replacement4 = string.Format("://{0}:{1}/ServiceGeneralQuery/tcp", serverip, "3118"); 56 address = Regex.Replace(address, pattern, replacement4); 57 if (node.Attributes != null) 58 { 59 node.Attributes["address"].InnerText = address; 60 } 61 break; 62 63 case "endpointDealData": 64 string replacement5 = string.Format("://{0}:{1}/ServiceDealData/tcp", serverip, "4118"); 65 address = Regex.Replace(address, pattern, replacement5); 66 if (node.Attributes != null) 67 { 68 node.Attributes["address"].InnerText = address; 69 } 70 break; 71 72 case "endpointSensorAlertData": 73 string replacement6 = string.Format("://{0}:{1}/ServiceSensorAlertData/tcp", serverip, "7118"); 74 address = Regex.Replace(address, pattern, replacement6); 75 if (node.Attributes != null) 76 { 77 node.Attributes["address"].InnerText = address; 78 } 79 break; 80 81 } 82 83 } 84 } 85 } 86 xmlDocument.Save("SIMU-Config.exe.config");89 } 90 }
上面代码 是我做了测试的。。有些地方写的有问题、、只是个思路。。。