日期:2014-05-18 浏览次数:20775 次
下面是一段源代码,个别地方需要修正 类变量 private static int total = 0; private int FileNumber = 0; //文件夹文件总数 调用 CopyDirectory(codeSourcePath + @"Site/",virtualDirPath + @"Site" + Path.DirectorySeparatorChar,statusForm); /// <summary> /// 复制文件夹 /// </summary> /// <param name="sourceDirName">源文件夹</param> /// <param name="destDirName">目标文件夹</param> /// <param name="statusWinForm">状态窗口</param> //复制文件夹 public void CopyDirectory( string sourceDirName, string destDirName,StatusWin statusWinForm) { if(!Directory.Exists(destDirName)) { Directory.CreateDirectory(destDirName); File.SetAttributes(destDirName, File.GetAttributes(sourceDirName)); //File.SetAttributes(destDirName,FileAttributes.Normal); } if (destDirName[destDirName.Length - 1] != Path.DirectorySeparatorChar) destDirName = destDirName + Path.DirectorySeparatorChar; string[] files = Directory.GetFiles(sourceDirName); foreach (string file in files) { File.Copy(file,destDirName + Path.GetFileName(file), true); File.SetAttributes(destDirName + Path.GetFileName(file),FileAttributes.Normal); total++; statusWinForm.progressBar1.Value = total; if(FileNumber == 0) { statusWinForm.lblStatus.Text = "已完成 100%"; } else { statusWinForm.lblStatus.Text = "已完成 " + (Math.Round((double)(100*total/FileNumber),0)).ToString() + "%"; } statusWinForm.lblSourceFile.Text = file; statusWinForm.lblFileName.Text = destDirName + Path.GetFileName(file); statusWinForm.lblStatus.Refresh(); statusWinForm.lblFileName.Refresh(); statusWinForm.lblSourceFile.Refresh(); } string[] dirs = Directory.GetDirectories(sourceDirName); foreach (string dir in dirs) { statusWinForm.Refresh(); statusWinForm.Focus(); statusWinForm.Activate(); CopyDirectory(dir, destDirName + Path.GetFileName(dir),statusWinForm); } } public static int GetFilesCount(System.IO.DirectoryInfo dirInfo) { int totalFile = 0; totalFile += dirInfo.GetFiles().Length; foreach (System.IO.DirectoryInfo subdir in dirInfo.GetDirectories()) { totalFile += GetFilesCount(subdir); } return totalFile; }