如何在安装了软件的多个文件夹中更新每个xml配置文件的IP地址

问题描述 投票:0回答:1

我有一个软件,安装后会询问我的服务器IP地址,并将该地址存储到不同文件夹中的多个配置文件中。我已使用此代码更新了一个配置文件中的配置文件的端点地址。

我将如何修改代码以查看软件的安装位置,并更新安装软件的不同文件夹内所有配置文件中的所有IP地址。

所有具有IP地址的配置文件看起来都与我的示例类似。

ConfigFile

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;


namespace ConfigTool
{
    class Class1
    {
    //    foreach (Environment.SpecialFolder folder_type in Enum.GetValues(typeof(Environment.SpecialFolder)))
    //    {
    //        DescribeFolder(folder_type);
    //    }
    //txtFolders.Select(0, 0);
    //const string FILENAME = Environment.GetFolderPath(Environment.SpecialFolder.);
        const string FILENAME = @"C:\Program Files (x86)\******\***\***\*****\*****.****.****.exe.config";
        public static IPAddress GetIPAddress(string hostName)
        {
            Ping ping = new Ping();
            var replay = ping.Send(hostName);

            if (replay.Status == IPStatus.Success)
            {
                return replay.Address;
            }
            return null;
        }

        static void Main(string[] args)
        {
            XDocument doc =  XDocument.Load(FILENAME);
                List<XElement> endpoints = doc.Descendants("endpoint").ToList();
                foreach (var endpoint in endpoints )
                {
                    string address = (string)endpoint.Attribute("address");
                    string newIp = "10.249.30.4";

                    string pattern = "//[^:]+";
                    address = Regex.Replace(address, pattern, "//" + newIp);

                    endpoint.Attribute("address").SetValue(address);
                }
            doc.Save(

XMLExample

<endpoint name="***Local" address="net.tcp://10.249.30.4:7732/EventSubscriberServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.ISubscriptionService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint name="***Local" address="net.tcp://10.243.32.4:7732/EventPublishServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.IPublishService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
        <identity>
          <dns value="*******" />
        </identity>
      </endpoint>
      <endpoint name="****" address="net.tcp://10.243.32.4:7732/AuthenticationServices/Secure" binding="netTcpBinding" contract="****.Services.ServiceContracts.IAuthenticationService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
c# xml visual-studio xml-parsing
1个回答
0
投票
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; namespace ConsoleApplication1 { class Program { const string FILENAME = @"c:\temp\test.xml"; static void Main(string[] args) { XDocument doc = XDocument.Load(FILENAME); List<EndPoint> endPoints = doc.Descendants("endpoint").Select(x => new EndPoint() { name = (string)x.Attribute("name"), address = (string)x.Attribute("address"), binding = (string)x.Attribute("binding"), contract = (string)x.Attribute("contract"), bindingConfiguration = (string)x.Attribute("bindingConfiguration"), behaviorConfiguration = (string)x.Attribute("behaviorConfiguration"), dns = (string)x.Descendants("dns").FirstOrDefault().Attribute("value"), }).ToList(); } } public class EndPoint { public string name { get; set; } public string address { get; set; } public string binding { get; set; } public string contract { get; set; } public string bindingConfiguration { get; set; } public string behaviorConfiguration { get; set; } public string dns { get; set; } } }
© www.soinside.com 2019 - 2024. All rights reserved.