How to Create a Directory in Any Drive

INTRODUCTION

Sometimes it is necessary to create a folder. In this article I explain how to determine if a directory (folder in a drive) exists. If it is exists then print "Directory exists" and if it does not exist then create it.The following steps are used to create the directory.

 Step 1: First open Visual Studio 2010 and click on file->new->project-> and click on console application, and press ok.

 Step 2: Add the following namespace for working with the directory.This Namespace is used for File Handling.

using System.IO;

Step 3: Write the following code in console application.

namespace ConsoleApplication1

{
    class
Program

    {
        const string name = "D:/rrr";

        static void Main(string[] args)

        {

            if (!Directory.Exists(name))

            {

                Directory.CreateDirectory(name);

                Console.WriteLine("{0} Directory create", name);

            }

            else

            {

                Console.WriteLine("{0} Directory exists", name);

            }

        }

    }

}


This program checks that the directory rrr in the D:/ drive exists or not; if not then create the directory.

Step 4: Since the directory does not exist the output is as:

directory_createed.jpg

Now go to the My Computer->D:/ Drive and check that the rrr folder now exists there.

Step 5: Now if I again run the program, the output is as:

exists.jpg

Since the directory now exists in the D:/ Drive, this output shows that the directory exists.

Summary: Now let's try to create the directory. In this example the drive is D. If you want to do that in another drive then specify the drive name in place of D:/ Drive.   


Similar Articles