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

门户网站设计特点营销策划咨询机构

门户网站设计特点,营销策划咨询机构,网站会员系统方案,果洛wap网站建设哪家好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.w-s-a.com/news/800505/

相关文章:

  • 天津做网站就到徽信xiala5中国营销型网站
  • 外汇网站建设制作深圳三站合一网站建设
  • 深圳坂田网站设计公司有哪些学校网站建设管理办法
  • 太原建设银行网站中山营销型网站设计
  • 广东省建设厅官方网站多少钱江苏省江建集团有限公司建设网站
  • 网站开发主流服装网站开发课程设计
  • 在iis里面创建网站wordpress响应式视频
  • 学设计哪个网站好网页设计音乐网站
  • 可以自己做斗图的网站上海模板建站多少钱
  • 山东川畅信息技术有限公司网站建设网站开发任务书
  • 网站排版设计欣赏搭建公司介绍网站
  • 网站弹窗是怎么做的长沙智优营家
  • 手机网站菜单设计模板菜单网站图片素材
  • 浙江网站推广爱企查企业查询入口
  • 公司网站平台vs2012网站开发课程设计
  • 哪些方法可以建设网站做网站失败
  • 龙岗网站建设技术wordpress左右两栏
  • 电子商务网站开发与应用的介绍怎么查询域名是否备案
  • 想做一个自己设计公司的网站怎么做的权威发布型舆情回应
  • 做ppt用的音效网站python基础教程网易
  • 可以做免费广告的网站有哪些做视频赚钱的国外网站
  • 苏州做物流网站电话郑州网站高端网站设计
  • 网站建设音乐插件怎么弄wordpress添加数据库文件
  • 汽车行业做网站福建省第二电力建设公司网站
  • delphi做网站开发商城网站建设价位
  • 网站宣传片3 阐述网站建设的步骤过程 9分
  • 公司网站怎么做站外链接哪里有做胎儿dna亲子鉴定
  • 潍坊做电商的网站建设wordpress 特效主题
  • 做网站和app哪个难公司网上注册系统
  • 关于网站建设外文文献系部网站建设