HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

If a folder does not exist, create it

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
existfolderdoesnotcreate

Problem

I use a FileUploader control in my application. I want to save a file to a specified folder. If this folder does not exist, I want to first create it, and then save my file to this folder. If the folder already exists, then just save the file in it.

How can I do this?

Solution

Use System.IO.Directory.CreateDirectory.

According to the official ".NET" docs, you don't need to check if it exists first.

System.io   >   Directory   >   Directory.CreateDirectory

Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. If the directory already exists, this method does not create a new directory, but it returns a DirectoryInfo object for the existing directory.
        — learn.microsoft.com/dotnet/api/

Context

Stack Overflow Q#9065598, score: 1562

Revisions (0)

No revisions yet.