How to create a directory in C#

To create a directory in C#, all you need is one line. Because the method CreateDirectory will create directories or folders, all the way down the tree. So this means that you don’t need to make any if statments to check if the directory exists. The method CreateDirectory will do that for you.

Directory.CreateDirectory("C:\\temp\\project1");
Create directory in C#
Create directory in C#

Using the method CreateDirectory is the best and fastest way to create folders/directories in C# and is widely used in my projects.

Check this post if you need to create a file in C#: https://www.jennerstrand.se/create-file-in-c/
Or look at this post to write lines to a text file: https://www.jennerstrand.se/write-lines-to-a-text-file-in-c-sharp/

Source on directory/folders from Microsoft: https://docs.microsoft.com/en-us/dotnet/api/system.io.directory?view=net-5.0

Check out these simple easy to read articles about add object to List, reverse for loops and for loops if you want som extra reading:
https://www.jennerstrand.se/add-object-to-list-in-c-sharp.
https://www.jennerstrand.se/reverse-for-loop-in-c-sharp.
https://www.jennerstrand.se/for-loop-in-c-sharp-simple.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please reload

Please Wait