您的位置:首页 > 其它

备份文件夹

2013-11-27 10:38 127 查看
自己写的一个备份文件夹得函数:

#region Back up the special folder
/// <summary>
/// Back up the special folder
/// </summary>
/// <param name="mainFolder"></param>
public static void BackUpDirectory(string mainFolder,string targetFolder)
{
DirectoryInfo mainDir = new DirectoryInfo(mainFolder);
//Get all files and Directorys in the special folder
FileSystemInfo[] filesADirs = mainDir.GetFileSystemInfos();
foreach (FileSystemInfo fileADir in filesADirs)
{
//back up sub directory
if (fileADir.GetType().Name == "DirectoryInfo")
BackUpDirectory(fileADir.FullName, fileADir.FullName.Replace(mainFolder, targetFolder));
else
{
//back file
//file new name
string fileNewName = fileADir.FullName.Replace(mainFolder, targetFolder);
string parentFolder = new FileInfo(fileNewName).DirectoryName;
//create new file's parent folder if it does't exists
if (!Directory.Exists(parentFolder))
Directory.CreateDirectory(parentFolder);
File.Copy(fileADir.FullName, fileNewName);
}
}
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: