Create a Directory Structure C#
Creating a Empty Directory Structure using C#, Directories and Sub directories with in specified source directory,
static void CreateDirectoryStructure(String szDir, String destDir)
{
try
{
//for C drive root path is C:\
//for D drive root path is D:\
//Replace Source path root with destination path root.
String destpathroot = Path.GetPathRoot(destDir);
String srcpathroot = Path.GetPathRoot(szDir);
String[] dirs = Directory.GetDirectories(szDir, "*", SearchOption.AllDirectories);
foreach (String difVolume in dirs)
{
String path = difVolume.Replace(srcpathroot, destpathroot);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
{
try
{
//for C drive root path is C:\
//for D drive root path is D:\
//Replace Source path root with destination path root.
String destpathroot = Path.GetPathRoot(destDir);
String srcpathroot = Path.GetPathRoot(szDir);
String[] dirs = Directory.GetDirectories(szDir, "*", SearchOption.AllDirectories);
foreach (String difVolume in dirs)
{
String path = difVolume.Replace(srcpathroot, destpathroot);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
for ex:
public static void Main()
{
//Creates a Empty Directory Structure of Inetpub located in C drive in D:\
CreateDirectoryStructure(@"c:\inetpub",@"d:\");
Console.Read();
}
Tags:Create a Directory Structure C#,Replicate folder Structure,Create Directory Structure,Folder Recursion,System.IO.Directory,System.IO.DirectoryInfo
No comments:
Post a Comment