Tuesday, September 23, 2014

Building File and Directory Paths

I see directory and file concatenation done with string append all too often. There are built in .Net libraries that help you do this so much easier. When you are working with file and directory paths, use System.IO.Path.Combine().
using System.IO;

namespace Tips
{
    class Program
    {
        static void Main(string[] args)
        {
            Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonPictures),
                "First SubDir",
                "Second Subdir",
                "MyFile.jpg");
        }
    }
}

No comments:

Post a Comment