Create a file from a path, creating subdirectories if they not exists

Viewed 16255

In config of my app I've a path, for example "logs\updater\updater.log"

Starting the app, I want to create the file updater.log, creating all subfolders if they not exists.

So, if tomorrow my user changes the path in config to "logs\mypathisbetter\updater.log", my app continues to work, writing log to the new file.

File.Create, FileInfo.Create, Streamwriter.Create or so: they do that?

Or do I need to check if folders exists, before?

I can't find a clear answer to this question on the net.

3 Answers

Another option if just want to create a folder/directory if it does not exist is to simply do this in C#:

// using System.IO;
if (!Directory.Exists(destination))
{
    Directory.CreateDirectory(destination);
}
Related