/// <summary> /// 移动图片文件夹,从原来的wwwroot/graphics文件夹转移到Picture文件夹 ///转移完毕删除原来的graphics文件夹 /// </summary> public void MoveGraphicsFolder() { try { // 定义源文件夹路径 string sourcePath = Path.Combine("wwwroot", "graphics"); // 定义目标文件夹路径 string targetPath = "Picture"; // 如果目标文件夹已存在,则删除它 string destinationGraphicsPath = Path.Combine(targetPath, "graphics"); if (!Directory.Exists(destinationGraphicsPath)) { Directory.CreateDirectory(destinationGraphicsPath); } // 确保目标目录存在,如果不存在则创建 if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } // 获取源目录和目标目录中的所有文件 string[] sourceFiles = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories); string[] targetFiles = Directory.GetFiles(destinationGraphicsPath, "*", SearchOption.AllDirectories); // 创建目标目录中不存在的目录 foreach (string sourceFile in sourceFiles) { string relativePath = Path.GetRelativePath(sourcePath, sourceFile); string targetFile = Path.Combine(destinationGraphicsPath, relativePath); string targetDirectory = Path.GetDirectoryName(targetFile)!; if (!Directory.Exists(targetDirectory)) { Directory.CreateDirectory(targetDirectory); } } // 处理每一个文件 foreach (string sourceFile in sourceFiles) { string relativePath = Path.GetRelativePath(sourcePath, sourceFile); string targetFile = Path.Combine(destinationGraphicsPath, relativePath); // 如果目标目录中已存在相同路径的文件,则进行覆盖 if (Array.Exists(targetFiles, f => string.Equals(f, targetFile, StringComparison.OrdinalIgnoreCase))) { File.Copy(sourceFile, targetFile, true); } // 如果目标目录中不存在相同路径的文件,则进行复制 else { File.Move(sourceFile, targetFile); } } // 删除源目录 Directory.Delete(sourcePath, true); } catch (Exception) { throw; } }
标签:里面,string,destinationGraphicsPath,targetFile,文件夹,Directory,Path,转移 From: https://www.cnblogs.com/ZkbFighting/p/18226027