The Below code copies the directory and all its sub directories from source to destination.
You have to mention the source path and Destination Path, if source path exist, first it copies the files and then folders...Recursively it calls the folder... And this is also an example for Recusive function
public void CopyDirectory(string SourcePath, string DestinationPath) { try {
if (Directory.Exists(SourcePath)) { foreach (string fls in Directory.GetFiles(SourcePath)) { FileInfo finfo = new FileInfo(fls); finfo.CopyTo(DestinationPath + "\\" + finfo.Name.ToString()); } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); if (!(Directory.Exists(DestinationPath + "\\" + drinfo.Name.ToString()))) { Directory.CreateDirectory(DestinationPath + "\\" + drinfo.Name.ToString()); CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString); } else { CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString()); }
}
} } catch (Exception ex) { MessageBox.Show(ex.Message); } }
|
| Author: Miss Meetu Choudhary 27 Aug 2009 | Member Level: Diamond Points : 2 |
Add Description To your Code...
|
| Author: Ramesh N D 29 Aug 2009 | Member Level: Gold Points : 2 |
Below code copies the directory and all its sub directory from source to destination.
you has to mention the source path and Destination Path, if source path exist, first it copies the files and go for floders...Recursively it calls the folder... And this is also an example for Recusive function
public void CopyDirectory(string SourcePath, string DestinationPath) { try {
if (Directory.Exists(SourcePath)) { foreach (string fls in Directory.GetFiles(SourcePath)) { FileInfo finfo = new FileInfo(fls); finfo.CopyTo(DestinationPath + "\\" + finfo.Name.ToString()); } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); if (!(Directory.Exists(DestinationPath + "\\" + drinfo.Name.ToString()))) { Directory.CreateDirectory(DestinationPath + "\\" + drinfo.Name.ToString()); CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString); } else { CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString()); }
}
} } catch (Exception ex) { MessageBox.Show(ex.Message); }
}
|
| Author: Ramesh N D 29 Aug 2009 | Member Level: Gold Points : 2 |
Below code copies the directory and all its sub directory from source to destination.
you has to mention the source path and Destination Path, if source path exist, first it copies the files and go for floders...Recursively it calls the folder... And this is also an example for Recusive function
public void CopyDirectory(string SourcePath, string DestinationPath) { try {
if (Directory.Exists(SourcePath)) { foreach (string fls in Directory.GetFiles(SourcePath)) { FileInfo finfo = new FileInfo(fls); finfo.CopyTo(DestinationPath + "\\" + finfo.Name.ToString()); } foreach (string drs in Directory.GetDirectories(SourcePath)) { DirectoryInfo drinfo = new DirectoryInfo(drs); if (!(Directory.Exists(DestinationPath + "\\" + drinfo.Name.ToString()))) { Directory.CreateDirectory(DestinationPath + "\\" + drinfo.Name.ToString()); CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString); } else { CopyDirectory(drs, DestinationPath + "\\" + drinfo.Name.ToString()); }
}
} } catch (Exception ex) { MessageBox.Show(ex.Message); }
}
|
| Author: Miss Meetu Choudhary 30 Aug 2009 | Member Level: Diamond Points : 2 |
PLease Format Your Resource. Don't add comments Edit This Resource
|