title: Replace the ServerName in a UNC Path
body: <p>I want to write a method to swap out the server name in a UNC path.  So if I have <code>"\\server1\folder\child"</code> I want to swap in <code>"\\server2\folder\child"</code>.  Various attempts to do this have run into jagged edges with .net's handling of backslashes (regex, Path.Combine).  I will not know the name of "server1" at runtime.</p>
  
  <p>Here's a snippet I have been testing in LinqPad, while it works it seems pretty hacky:</p>
  
  <pre><code>string path = @"\\server1\folder\child";
  
  var uri = new Uri(path);
  
  string newPath = @"\\server2\";
  
  foreach (var part in uri.Segments)
  {
   if (part == "/")
       continue;
  
   newPath += part;
  }
  
  var x = new Uri(newPath);
  
  uri.Dump();
  
  x.LocalPath.Dump();
  </code></pre>