Create Folder and Subfolder at Run Time

Posted by Lakhangarg under C# category on | Views : 3127
Create Instance of DirectoryInfo To Create Directories.
Pass the Page object so that we can the the serverpath
Directory.CreateDirectory({path}) is Used to create Parent Directory.

and Further we can use :
childDir =parentDir.CreateSubdirectory("Child");
to create child Directory inside the Parent Folder

public static void FolderCreation(Page page)
//variable declaration
string serverPath = null;
string virtualServerPath = null;
DirectoryInfo parentDir = null;
DirectoryInfo childDir = null;
//getting full server path
serverPath = page.Server.MapPath(".");
//server path -1 step
virtualServerPath = serverPath.Substring(0, serverPath.LastIndexOf("\\"));
virtualServerPath = virtualServerPath + @"\MyFolder\Parent";

//create the directory with the name Parent in MyFolder
parentDir = Directory.CreateDirectory(virtualServerPath);

//Create Child Directory inside Parent folder
childDir =parentDir.CreateSubdirectory("Child");

Comments or Responses

Login to post response