// 修改system.serviceModel下所有服务终结点的IP地址
public static void UpdateServiceModelConfig(string configPath, string serverIP)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(configPath);
ConfigurationSectionGroup sec = config.SectionGroups["system.serviceModel"];
ServiceModelSectionGroup serviceModelSectionGroup = sec as ServiceModelSectionGroup;
ClientSection clientSection = serviceModelSectionGroup.Client;
foreach (ChannelEndpointElement item in clientSection.Endpoints)
{
string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b";
string address = item.Address.ToString();
string replacement = string.Format("{0}", serverIP);
address = Regex.Replace(address, pattern, replacement);
item.Address = new Uri(address);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">system.serviceModel</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 修改applicationSettings中App.Properties.Settings中服务的IP地址</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> UpdateConfig(<span style="color: rgba(0, 0, 255, 1)">string</span> configPath, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serverIP)
{
Configuration config </span>=<span style="color: rgba(0, 0, 0, 1)"> ConfigurationManager.OpenExeConfiguration(configPath);
ConfigurationSectionGroup sec </span>= config.SectionGroups[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">applicationSettings</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
ConfigurationSection configSection </span>= sec.Sections[<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataService.Properties.Settings</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">];
ClientSettingsSection clientSettingsSection </span>= configSection <span style="color: rgba(0, 0, 255, 1)">as</span><span style="color: rgba(0, 0, 0, 1)"> ClientSettingsSection;
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (clientSettingsSection != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
SettingElement element1 </span>= clientSettingsSection.Settings.Get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataService_SystemManagerWS_SystemManagerWS</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (element1 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
clientSettingsSection.Settings.Remove(element1);
</span><span style="color: rgba(0, 0, 255, 1)">string</span> oldValue =<span style="color: rgba(0, 0, 0, 1)"> element1.Value.ValueXml.InnerXml;
element1.Value.ValueXml.InnerXml </span>=<span style="color: rgba(0, 0, 0, 1)"> GetNewIP(oldValue, serverIP);
clientSettingsSection.Settings.Add(element1);
}
SettingElement element2 </span>= clientSettingsSection.Settings.Get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">DataService_EquipManagerWS_EquipManagerWS</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (element2 != <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
{
clientSettingsSection.Settings.Remove(element2);
</span><span style="color: rgba(0, 0, 255, 1)">string</span> oldValue =<span style="color: rgba(0, 0, 0, 1)"> element2.Value.ValueXml.InnerXml;
element2.Value.ValueXml.InnerXml </span>=<span style="color: rgba(0, 0, 0, 1)"> GetNewIP(oldValue, serverIP);
clientSettingsSection.Settings.Add(element2);
}
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">applicationSettings</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
}
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">string</span> GetNewIP(<span style="color: rgba(0, 0, 255, 1)">string</span> oldValue, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> serverIP)
{
</span><span style="color: rgba(0, 0, 255, 1)">string</span> pattern = <span style="color: rgba(128, 0, 0, 1)">@"</span><span style="color: rgba(128, 0, 0, 1)">\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">string</span> replacement = <span style="color: rgba(0, 0, 255, 1)">string</span>.Format(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">{0}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, serverIP);
</span><span style="color: rgba(0, 0, 255, 1)">string</span> newvalue =<span style="color: rgba(0, 0, 0, 1)"> Regex.Replace(oldValue, pattern, replacement);
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> newvalue;
}</span></pre>