The World’s Leading Microsoft .NET Magazine
   
 
timstall

Donate Today!

Search Box

 

Calendar

««Jul 2008»»
SMTWTFS
  
1
2
3
4
5
6789101112
13141516171819
20212223242526
2728293031

My RSS Feeds








Mailing List

Most Popular Tags

                                       

Using Path.Combine instead of string concatenation

posted Wednesday, 5 December 2007

I often need to work with filePaths, such as getting an absolute file name given a directory and a fileName. One way for developers to do this is simply string concatenation. A lot of devs do this because it appears so simple. But the problem is that when passing in the directory as a string, you often don't know if it will end with a final slash or not - i.e "C:\Temp" or "C:\Temp\". Merely doing string concat will screw it up, you could get a non-existent file like "C:\Temp\\MyFile.txt".

 

A much better approach is to use System.IO.Path.Combine. This allows you to have a directory (with or without the slash), and the combine method if smart enough to handle it:


    public static string GetFileName(string strDir, string strName)
    {
      //return strDir + @"\" + strName;   //BAD
      return System.IO.Path.Combine(strDir, strName); //GOOD
    }

 

What's funny is that both options are just 1 line of code and take essentially the exact same amount of time to write. However, the first is very brittle, and could likely cost you tons of time tracking down an error because someone passed in a directory with or without that extra slash. The second just works.

tags:  

links: digg this    technorati    




1. Jeff left...
Wednesday, 5 December 2007 3:00 pm

I am not having a lot of luck with this maybe you can elaborate further. I always concatenate in this situation but make sure I cleanse the data going in so I know where my slashes are, my simple tests on this actually show this to be a pretty poor solution. So maybe you can clarify. Note my method I use would cleans the dorectories passes in and coming out they would all be the same.

Simple Console application in the Main Method every test line give you a completely different directory or path line.

  • string baseDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

  • string tempDirectory;

  • Console.WriteLine("Application Domain Base Directory");

  • Console.WriteLine(baseDirectory);

  • tempDirectory = System.IO.Path.Combine(baseDirectory, "/test");

  • Console.WriteLine(tempDirectory);

  • tempDirectory = System.IO.Path.Combine(baseDirectory, "\\test");

  • Console.WriteLine(tempDirectory);

  • tempDirectory = System.IO.Path.Combine(baseDirectory, "/test/");

  • Console.WriteLine(tempDirectory);

  • tempDirectory = System.IO.Path.Combine(baseDirectory, "/test\\");

  • Console.WriteLine(tempDirectory);

  • tempDirectory = System.IO.Path.Combine(baseDirectory, "test");

  • Console.WriteLine(tempDirectory);

  • tempDirectory = System.IO.Path.Combine(baseDirectory, "test\\");

  • Console.WriteLine(tempDirectory);


2. Tim Stall left...
Wednesday, 5 December 2007 3:27 pm

Hey Jeff, I'm not sure what the exact question is. If you just want to combine two directories, I think it's just this (no slashes needed) tempDirectory = System.IO.Path.Combine(baseDirectory, "test");


3. Chris R. Timmons left...
Wednesday, 5 December 2007 5:53 pm

I think Jeff's question is about a documented - but very annoying - behavior of Path.Combine that I've run into before.

From the .Net 2.0 help entry for Path.Combine (remarks section) regarding the second parameter (path2):

  • "If path2 includes a root, path2 is returned."

That means Path.Combine("c:\test", "\path") will return "\path", not "c:\test\path", because Path.IsPathRooted("\path") returns true.

Makes me wonder what the original author of Path.Combine was thinking...


4. Tim Stall left...
Thursday, 6 December 2007 9:48 am

That is strange. Could you solve this by applying a check to path2, and stripping off any potentially-leading slash. I.e. "\path" becomes just "path". You could then wrap it up as your own utility function.