当前位置: 首页 > news >正文

门户网站分类织梦做泰文网站

门户网站分类,织梦做泰文网站,顺德网站建设教程,制作网站要多久C# 备份目标文件夹 方法1#xff1a;通过 递归 或者 迭代 结合 C# 方法 参数说明#xff1a; sourceFolder#xff1a;源文件夹路径destinationFolder#xff1a;目标路径excludeNames#xff1a;源文件夹中不需备份的文件或文件夹路径哈希表errorLog#xff1a;输出错…C# 备份目标文件夹 方法1通过 递归 或者 迭代 结合 C# 方法 参数说明 sourceFolder源文件夹路径destinationFolder目标路径excludeNames源文件夹中不需备份的文件或文件夹路径哈希表errorLog输出错误log 递归实现 private bool CopyAllFolder(string sourceFolder, string destinationFolder, HashSetstring excludeNames, out string errorLog){errorLog string.Empty;try{if (!Directory.Exists(destinationFolder)){Directory.CreateDirectory(destinationFolder);}string[] directories Directory.GetDirectories(sourceFolder);string[] files Directory.GetFiles(sourceFolder);foreach (string file in files){if (excludeNames.Count ! 0 excludeNames.Contains(file)){continue;}try{if (!BRTools.IsFileReady(file) || !BRTools.IsNotFileInUse(file, out errorLog)) // 检测文件是否被占用{return false;}string destinationFile Path.Combine(destinationFolder, Path.GetFileName(file));File.Copy(file, destinationFile, true);}catch (Exception ex){errorLog $Error copying file {file}: {ex.Message}\n;return false;}}foreach (string directory in directories){if (excludeNames.Count ! 0 excludeNames.Contains(directory)){continue;}string destinationSubFolder Path.Combine(destinationFolder, Path.GetFileName(directory));if (!CopyAllFolder(directory, destinationSubFolder, excludeNames, out string subfolderErrorLog)){errorLog subfolderErrorLog;return false;}}return true;}catch (Exception ex){errorLog $Error during folder copy: Message {ex.Message}, StackTrace {ex.StackTrace}\n;return false;}}迭代实现 private bool CopyAllFolder(string sourceFolder, string destinationFolder, HashSetstring excludeNames, out string errorLog){errorLog string.Empty;try{if (!Directory.Exists(destinationFolder)){Directory.CreateDirectory(destinationFolder);}Stackstring directoryStack new Stackstring();directoryStack.Push(sourceFolder);while (directoryStack.Count 0){string currentDirectory directoryStack.Pop();string[] directories Directory.GetDirectories(currentDirectory);string[] files Directory.GetFiles(currentDirectory);foreach (string file in files){if (excludeNames.Count ! 0 excludeNames.Contains(file)){continue;}try{if (!BRTools.IsFileReady(file) || !BRTools.IsNotFileInUse(file, out errorLog)){return false;}string destinationFile Path.Combine(destinationFolder, Path.GetFileName(file));File.Copy(file, destinationFile, true);}catch (Exception ex){errorLog $Error copying file {file}: {ex.Message}\n;return false;}}foreach (string directory in directories){if (excludeNames.Count ! 0 excludeNames.Contains(directory)){continue;}string destinationSubFolder Path.Combine(destinationFolder, Path.GetFileName(directory));if (!CopyAllFolder(directory, destinationSubFolder, excludeNames, out string subfolderErrorLog)){errorLog subfolderErrorLog;return false;}directoryStack.Push(directory);}}return true;}catch (Exception ex){errorLog $Error during folder copy: Message {ex.Message}, StackTrace {ex.StackTrace}\n;return false;}}方法2利用 Windows API [DllImport(shell32.dll, CharSet CharSet.Auto)]public static extern int SHFileOperation(ref SHFILEOPSTRUCT lpFileOp);[StructLayout(LayoutKind.Sequential, CharSet CharSet.Auto)]public struct SHFILEOPSTRUCT{public IntPtr hwnd;public int wFunc;public string pFrom;public string pTo;public short fFlags;public bool fAnyOperationsAborted;public IntPtr hNameMappings;}const int FO_COPY 0x0002;const int FOF_NOCONFIRMATION 0x0010;const int FOF_SILENT 0x0004;const int FOF_NO_UI FOF_NOCONFIRMATION | FOF_SILENT;private bool CopyDirectory(string sourceDir, string destDir, out string errorLog){errorLog string.Empty;try{SHFILEOPSTRUCT fileOp new SHFILEOPSTRUCT();fileOp.wFunc FO_COPY;fileOp.pFrom sourceDir \0 \0; // Must end with double null characterfileOp.pTo destDir \0 \0; // Must end with double null character//fileOp.fFlags FOF_NO_UI;fileOp.fFlags FOF_NO_UI | FOF_NOCONFIRMATION; // 忽略UI和确认对话框int result SHFileOperation(ref fileOp);// 检查返回值if (result ! 0){errorLog $SHFileOperation failed with error code: {result};return false;}return true;}catch (Exception ex){errorLog $Failed to copy the entire folder {sourceDir}: Message {ex.Message}, StackTrace {ex.StackTrace}\n;return false;}}private bool CopyFolder(string sourceFolder, string destinationFolder, HashSetstring excludeNames, out string errorLog){errorLog string.Empty;try{if (!CopyDirectory(sourceFolder, destinationFolder, out errorLog)){this.logger.Warning($errorLog: {errorLog});return false;}if (excludeNames.Count ! 0){foreach (var item in excludeNames){var targetPath Path.Combine(destinationFolder, GetSonFolderPath(sourceFolder, item)); // 获取已备份路径下需排除的文件夹或文件路径if (Directory.Exists(item)){ DeleteDir(targetPath);}if(File.Exists(item)){DeleteDir(targetPath);}}}return true;}catch(Exception ex){errorLog $Error during folder copy, and exception is: Message {ex.Message}, StackTrace {ex.StackTrace}\n;return false;}}private string GetSonFolderPath(string folderPath, string targetPath){string result string.Empty;try{folderPath folderPath.TrimEnd(Path.DirectorySeparatorChar) Path.DirectorySeparatorChar;if (!isFilePath(targetPath)){targetPath targetPath.TrimEnd(Path.DirectorySeparatorChar) Path.DirectorySeparatorChar;}else{targetPath Path.GetDirectoryName(targetPath).TrimEnd(Path.DirectorySeparatorChar);}if (targetPath.StartsWith(folderPath, StringComparison.OrdinalIgnoreCase)){result targetPath.Substring(folderPath.Length);}}catch (Exception){result string.Empty;}return result;}private bool isFilePath(string targetPath){if (Path.HasExtension(targetPath) File.Exists(targetPath))return true;return false;}private void DeleteFile(string file){if (File.Exists(file)){FileInfo fi new FileInfo(file);if (fi.IsReadOnly){fi.IsReadOnly false;}File.Delete(file);}}private void DeleteDir(string dir){if (Directory.Exists(dir)){foreach (string childName in Directory.GetFileSystemEntries(dir)){if (File.Exists(childName)){FileInfo fi new FileInfo(childName);if (fi.IsReadOnly){fi.IsReadOnly false;}File.Delete(childName);}elseDeleteDir(childName);}Directory.Delete(dir, true);}}注意方法2有一个漏洞该方法无法成功捕捉到源文件夹下被占用的文件信息
http://www.sczhlp.com/news/164951/

相关文章:

  • 做食品检测的网站网络艺术设计是什么
  • 搭建一个网站多少钱网站反链一般怎么做
  • 云南省住房城乡建设厅网站软件合集大全
  • 黄石网站建设教程汽配网站开发
  • 网站建设 三牛请seo的人帮做网站排名
  • 新县城乡规划建设局网站hyip系统网站开发
  • 怎么制作网站首页网站运营做哪些工作呢
  • 做网站主机几个配件济南网站建设设计公司
  • 公司怎样制作网站虚拟网站怎么做的
  • 门户网站大全网站建设怎么报价
  • 有哪些免费网站可以做店招建设企业网站要多少钱
  • 中企动力网站开发室内装饰设计平面图
  • 网站建设与制作模板比较好的网站开发框架
  • 手机网站有什么好处做网站的需要考什么证书吗
  • 网站建设高端网页设计营销网点是什么意思
  • 网站域名是不是网址网站建设利润越来越低
  • wordpress数据查询网站大宗贸易交易平台
  • 柳州网站建设哪家公司好做加盟网站哪个最好
  • 网站建设配置文件无法粘贴怎样申请建网站
  • dedecms怎么做网站网站重定向怎么做
  • wordpress 网站加密插件网站建设技术员保密协议
  • 外贸公司网站制作公司惠州网站制作软件
  • 西宁网站seo公司珠海正规网站制作哪家强
  • 第三方编辑网站怎么做wordpress订阅功能
  • 温岭营销型网站建设如何推广营销一个项目
  • 做跟单员的话应该关注哪些网站中国软件公司排行
  • 玩转树莓派屏幕之一:LCD屏幕显示
  • Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
  • 企业网站托管收费标准湖北微网站建设电话
  • 普通网站要什么费用关键词网站建设优化