title: C# combining absolute and relative paths, Path.GetFullPath() inconsistent
body: <p>I notice that when I use Path.GetFullPath(Path.Combine(@"c:\folder\","\subfolder\file.txt")) it returns a path c:\subfolder\file.txt, instead of the expected combined path c:\folder\subfolder\file.txt. It seems like having a '\' on the second combined input isn't handled by these methods. Has anyone come across this and found a better solution for merging paths?</p>
  
  <pre><code>var test1 = Path.GetFullPath(Path.Combine(@"C:\users\dev\desktop\testfiles\", @".\test1.txt"));
  var test2 = Path.GetFullPath(Path.Combine(@"C:\users\dev\desktop\testfiles\", @"\test2.txt"));//
  var test3 = Path.GetFullPath(Path.Combine(@"C: \users\dev\desktop\testfiles\", @"test3.txt"));
  var test4 = Path.GetFullPath(Path.Combine(@"C:\users\dev\desktop\testfiles\",@".\XXX\test4.txt"));
  var test5 = Path.GetFullPath(Path.Combine(@"C:\users\dev\desktop\testfiles\", @"\XXX\test5.txt"));//
  var test6 = Path.GetFullPath(Path.Combine(@"C:\users\dev\desktop\testfiles\", @"XXX\test6.txt"));
  var test7 = Path.GetFullPath(Path.Combine(@"C:\users\dev\desktop\testfiles\", @"\somefile\that\doesnt\exist.txt"));//
  </code></pre>
  
  <p>Results in</p>
  
  <pre><code>test1 is "C:\users\dev\desktop\testfiles\test1.txt"
  test2 is wrong "\test2.txt"
  test3 is "C: \users\dev\desktop\testfiles\test3.txt"
  test4 is "C:\users\dev\desktop\testfiles\XXX\test4.txt"
  test5 is wrong "c:\XXX\test5.txt"
  test6 is "C:\users\dev\desktop\testfiles\XXX\test6.txt"
  test7 is wrong "c:\somefile\that\doesnt\exist.txt"
  </code></pre>
  
  <p>Basically what I'm doing is combining a source path with individual file paths from a text file so that I can copy these files to a destination, and I want to maintain subfolder hierarchy.</p>
  
  <p>e.g. common scenario of the files list being the same, but wanting to reference a specific source folder version.</p>
  
  <pre><code>source path + individual file path
  \\myserver\project\1.x + \dll\important.dll
  </code></pre>
  
  <p>which should be copied to a destination in the same manner</p>
  
  <pre><code>c:\myproject\ + \dll\important.dll
  </code></pre>
  
  <p>So there would be variables; source, destination, files[array or list]</p>
  
  <p>My current way forward is to use String.TRIM() method to remove special chars . \ / before combining paths with Path.Combine, Path.GetFullPath. But I thought maybe someone has realized a better way for a similar scenario.</p>
  
  <p>It's also a pain creating the subfolders in the destination, basically im using String.LastIndexOf(@'\') to separate the filename from the foldername. Which is also seems a little sketchy, but a better way is beyond my experience. </p>
  
  <p>Similar question:
  <a href="https://stackoverflow.com/questions/670566/path-combine-absolute-with-relative-path-strings?lq=1">Path.Combine absolute with relative path strings</a></p>
  
  <hr>
  
  <p>Thanks to the feedback I now know that \file.txt is considered a fully qualified (absolute) path, which is why the combine method works that way.</p>
  
  <p>A file name is relative to the current directory if it does not begin with one of the following:</p>
  
  <pre><code>A UNC name of any format, which always start with two backslash characters ("\\"). For more information, see the next section.
  A disk designator with a backslash, for example "C:\" or "d:\".
  A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.
  </code></pre>
  
  <p><a href="https://msdn.microsoft.com/en-nz/library/windows/desktop/aa365247(v=vs.85).aspx#fully_qualified_vs._relative_paths" rel="nofollow noreferrer">https://msdn.microsoft.com/en-nz/library/windows/desktop/aa365247(v=vs.85).aspx#fully_qualified_vs._relative_paths</a></p>