How to Create a Folder in the Current Directory Using C#?

Do you need to create a folder in the current directory in C#.Net? This is a very common requirement in most of the C# projects. In this tutorial, I have explained different methods to create a folder in the current directory using C#.

To create a folder in the current directory using C#, utilize the Directory.CreateDirectory method combined with Directory.GetCurrentDirectory(). This combination allows you to specify the folder name while automatically setting the location to the current working directory of your C# application.

To create a folder in the current directory using C#, you can use one of the below two methods:

  • Using the Directory Class
  • Using DirectoryInfo Class

Also, we will see how to check if the folder already exists before creating the folder in C#.

Create a Folder in the Current Directory Using C#

Now, let us check with examples of creating a folder in the current directory in C#.

Using the Directory Class

One of the easiest ways to create a folder in the current directory is by using the Directory class in C#, which is part of the System.IO namespace.

Here is a simple example:

using System;
using System.IO;

namespace CreateFolderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderName = "NewFolder";

            // Create new directory in the current directory
            Directory.CreateDirectory(folderName);

            Console.WriteLine($"Folder '{folderName}' has been created.");
        }
    }
}

In the above example, we use the CreateDirectory method from the Directory class to create a new folder named “NewFolder” in the current directory.

Using DirectoryInfo Class

Another approach to creating a folder is using the DirectoryInfo class in C#, also part of the System.IO namespace.

Here’s how you can do it:

using System;
using System.IO;

namespace CreateFolderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderName = "NewFolder";
            DirectoryInfo directoryInfo = new DirectoryInfo(folderName);

            // Create new directory in the current directory
            directoryInfo.Create();

            Console.WriteLine($"Folder '{folderName}' has been created.");
        }
    }
}

In this example, we instantiate a DirectoryInfo object and then call its Create method to create the folder.

It’s good practice to check whether the folder already exists before creating it. Here’s how you can do that using the Directory class:

if (!Directory.Exists(folderName))
{
    Directory.CreateDirectory(folderName);
    Console.WriteLine($"Folder '{folderName}' has been created.");
}
else
{
    Console.WriteLine($"Folder '{folderName}' already exists.");
}

Complete C# Program Example

Here is a complete example using both Directory and DirectoryInfo classes to create a folder in the current directory in C# along with validation:

using System;
using System.IO;

namespace CreateFolderExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderName = "NewFolder";

            // Validate and create directory using Directory class
            if (!Directory.Exists(folderName))
            {
                Directory.CreateDirectory(folderName);
                Console.WriteLine($"Folder '{folderName}' has been created using Directory class.");
            }
            else
            {
                Console.WriteLine($"Folder '{folderName}' already exists.");
            }

            folderName = "AnotherNewFolder";
            DirectoryInfo directoryInfo = new DirectoryInfo(folderName);

            // Validate and create directory using DirectoryInfo class
            if (!directoryInfo.Exists)
            {
                directoryInfo.Create();
                Console.WriteLine($"Folder '{folderName}' has been created using DirectoryInfo class.");
            }
            else
            {
                Console.WriteLine($"Folder '{folderName}' already exists.");
            }
        }
    }
}

Once you run the code using a console or a Windows application in C#, you can see the output below; the two folders have been created in the current directory.

Create a Folder in the Current Directory Using C#

Conclusion

Creating a folder in the current directory using C# is very easy and straightforward. Here, I have explained how to use the Directory class or DirectoryInfo class to create a folder in C# in the current directory. It is always good to validate if the folder already exists to avoid any potential errors.

You may also like: