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)