How do I create a file AND any folders, if the folders don't exist?
To summarize what has been commented in other answers:
//path = @"C:\Temp\Bar\Foo\Test.txt";
Directory.CreateDirectory(Path.GetDirectoryName(path));
Directory.CreateDirectory
will create the directories recursively and if the directory already exist it will return without an error.
If there happened to be a file Foo
at C:\Temp\Bar\Foo
an exception will be thrown.
DirectoryInfo di = Directory.CreateDirectory(path);
Console.WriteLine("The directory was created successfully at {0}.",
Directory.GetCreationTime(path));
See this MSDN page.
Hope that helps out!
Use Directory.CreateDirectory before you create the file. It creates the folder recursively for you.