Processing files with C# in folders whose names contain spaces

Posted by Nigel Ainscoe on Stack Overflow See other posts from Stack Overflow or by Nigel Ainscoe
Published on 2010-05-08T17:26:02Z Indexed on 2010/05/08 17:38 UTC
Read the original article Hit count: 342

Filed under:

There are plenty of C# samples that show how to manipulate files and directories but they inevitably use folder paths that contain no spaces. In the real world I need to be able to process files in folders with names that contain spaces. I have written the code below which shows how I have solved the problem. However it doesn't seem to be very elegant and I wonder if anyone has a better way.

    class Program
{
    static void Main(string[] args)
    {

        var dirPath = @args[0] +  "\\";

        string[] myFiles = Directory.GetFiles(dirPath, "*txt");
        foreach (var oldFile in myFiles)
        {
            string newFile = dirPath + "New " + Path.GetFileName(oldFile);
            File.Move(oldFile, newFile);
        }
        Console.ReadKey();
    }
}

Regards, Nigel Ainscoe

© Stack Overflow or respective owner

Related posts about c#