How to create a folder or directory in C#

This code shows how to create a new folder or directory. Copy this code to your application and call this method by passing the full path of your new folder you would like to create.

/// <summary>

        /// This method creates and delete a directory

        /// Checks if a directory already exists and also

        /// checks the time directory was created on

        /// </summary>

        public void CreateDirectory(string DirectoryName)

        {

            try

            {

                // Determine whether the directory exists.

                if (Directory.Exists(DirectoryName))

                {

                    Console.WriteLine("Directory already exists.");

                    return;

                }

 

                // Create directory

                DirectoryInfo di = Directory.CreateDirectory(DirectoryName);

                Console.WriteLine("Directory created on " + Directory.GetCreationTime(DirectoryName));

 

                // Delete directory

                di.Delete();

                Console.WriteLine("The directory was deleted successfully.");

            }

            catch (Exception exp)

            {

                Console.WriteLine("The process failed: {0}", exp.ToString());

            }        

        }