title: How to loop through Folders and update XML Files
body: <p>I have a software that upon installation asks for the IP address of my server and stores that address into multiple config files in different folders.</p>
  
  <p>I'm still kind of new to C# but I created a utility where if for whatever reason their IP address were to change or they want to change it, the IP address that was stored will update to the current IP address.</p>
  
  <pre><code>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
      {
          //Not sure if this would work since this is just a path to a bunch of folders and not the actual xml files within those folders
          const string FILENAME = @"C:\Program Files (x86)\Stuff\Noodles"; 
  
          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); 
              var path = Directory.EnumerateFiles(@"C:\Program Files (x86)\Stuff\Noodles", "*.config");
              foreach (var xmlfile in path)
              {
                  XDocument doc = XDocument.Load(xmlfile);
                  List&lt;XElement&gt; 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, "//" + GetIPAddress(Dns.GetHostName()));
  
                      endpoint.Attribute("address").SetValue(address);
                  }
                  doc.Save(FILENAME);
              }
          }
      }
  }
  </code></pre>
  
  <p>My code is filtering all the xml config files within the directory and updating it with a new IP address. However their are some xml config files in certain folders that I DON'T want to change like the ones that say net.tcp://localhost. </p>
  
  <pre><code>&lt;endpoint name="SessionLocal" address="net.tcp://localhost:7732/AuthenticationServices/Secure" binding="netTcpBinding" contract="WelchAllyn.Services.ServiceContracts.IAuthenticationService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost"/&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
        &lt;endpoint name="DataLocal" address="net.tcp://localhost:7732/DataServices/Secure" binding="netTcpBinding" contract="WelchAllyn.Services.ServiceContracts.IDataService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost"/&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
  </code></pre>
  
  <p>The xml files that I want to change have actual IP addresses like</p>
  
  <pre><code>&lt;endpoint name="SubscriptionLocal" address="net.tcp://10.249.30.4:7732/EventSubscriberServices/Secure" binding="netTcpBinding" contract="WelchAllyn.Services.ServiceContracts.ISubscriptionService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost" /&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
        &lt;endpoint name="PublishLocal" address="net.tcp://10.249.30.4:7732/EventPublishServices/Secure" binding="netTcpBinding" contract="WelchAllyn.Services.ServiceContracts.IPublishService" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="CustomValidator"&gt;
          &lt;identity&gt;
            &lt;dns value="localhost" /&gt;
          &lt;/identity&gt;
        &lt;/endpoint&gt;
  </code></pre>
  
  <p>My questions are how do I only update IP addresses with actual IP addresses and not the ones with localhost. And how would I loop through every folder within the directory and save each xml file within each folder?</p>