Processing files with C# in folders whose names contain spaces
- by Nigel Ainscoe
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