How do you create a folder in c#?

I have a script to make a text file, but if the folder does not exist, it won’t create the text file.

As Eric5h5 said, you have to first:

using System.IO

Then, if you want to create a Folder:

var folder = Directory.CreateDirectory("path/to/your/dir"); // returns a DirectoryInfo object

if you wanna create a file:

var file = File.Create("path/to/your/file"); // returns a FileInfo object

I have a nice tutorial package especially for the System.IO namespace, you’ll learn everything you need about handling files and folders, have a look :slight_smile:

Because of the static nature of Directory class , we do not have to instantiate the class. We can call the methods directly from the Directory class itself.

  try
  {
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }

  }
  catch (IOException ex)
  {
     Console.WriteLine(ex.Message)
  }

Source : Directory Class

Justin

Anything related to file IO is always in System.IO.