Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Create folder

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 66
    Comment on it

    To Create a new folder we use a Directory.CreateDirectory() method. This method allows us to create new directories in the specified path only if the specified path is not already exists. It is one of a static method in Directory class because of its static nature we do not have to make a object for this class.

    Syntax:

    public static DirectoryInfo CreateDirectory(string path)

    It accept one parameter "path" which is type of System.String.It specify the directory to create.

    C# Code :

    Directory.CreateDirectory("c:\\Test1");

    To create a directory or folder, we must check whether the directory or folder is already exist or not. In C#, we have an Exists method in the Directory class which is use to check the directory existence.

    Syntax : bool Directory.Exist(string FolderName)

    It accept one parameter "FolderName" which define the name of the directory.

    Its return type is bool type value .i.e. If directory exists then, it Returns true otherwise, it Returns false.

    C# Code :

    Directory.Exists("c:\\Test1")

    Add the following namespace:

    using System;

    using System.IO;

    A sample C# program to create a folder

    class SampleProgram
        {
            static void Main(string[] args)
            {
                string Fname = @"D:\myNewFolder";            
                try
                {
                    if (!Directory.Exists(Fname))
                    {
                        Directory.CreateDirectory(Fname);
                        Console.WriteLine("Path of new folder: {0}\n", Fname);
                        Console.ReadKey();
                    }
                    else
                    {
                        Console.WriteLine("Folder name \"{0}\" already exists.",Fname);
                        Console.ReadKey();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Failed: {0}", e.ToString());
                }
            }
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: