Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C# : How to create a folder if it does not exist ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 193
    Comment on it

    Many times in application development using C# it is required to create a folder if it does not exist.

    We can use the below method of C# to accomplish the same:

    System.IO.Directory.CreateDirectory(folderPath);

    Following is the code for creation of folder:

    using System.IO;
    
    private void CreateFolder(string folderPath)
    {
      bool folderExists = Directory.Exists(Server.MapPath(folderPath));
    
      if (!folderExists)
        Directory.CreateDirectory(Server.MapPath(folderPath));
    }
    

    If the folder already exists, CreateDirectory does nothing, and no exception is thrown.

    The following conditions may cause an exception:

    1) The folder name is malformed. For example, it contains illegal characters or is only white space (ArgumentException class). Use the Path class to create valid path names.

    2) The parent folder of the folder to be created is read-only (IOException class).

    3) The folder name is null (ArgumentNullException class).

    4) The folder name is too long (PathTooLongException class).

    5) The folder name is only a colon, ":" (PathTooLongException class).

    Ref: https://msdn.microsoft.com/library/as2f1fez(v=vs.110).aspx

 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: