文件夹移动复制删除后出现找不到已删文件夹的错误排查
问题:文件夹操作后出现找不到已删除文件夹的异常
问题背景
我要实现的操作是:移动某个文件夹位置,再把另一个文件夹的内容复制到父目录并删除原文件夹。这些操作本身能正常完成,但流程结束后会抛出异常,提示找不到已删除的文件夹,搞不清为啥程序在文件夹删除后还去查找它。
异常详情
异常指向代码行:Directory.Move(FoundFolder, Path.GetFullPath(Setup.SelectedPath) + @"\\" + Path.GetFileName(FoundFolder));
错误信息:
找不到路径的一部分 'X:\Games\GameName\GameName'
现有代码
string[] SetupFolderKeywords = { "Setup", "Installed" }; //keywords string[] roots = Directory.GetDirectories(Game.SelectedPath, "*", SearchOption.TopDirectoryOnly); //gets all directories in Game.SelectedP foreach (var item in roots) //foreach directory found { string? FoundFolder = Path.GetFullPath(item); //We get each path for each directory found if (FoundFolder.Contains(SetupFolderKeywords[1])) //if any of the found directories have any of the keywords { CopyDeleteFolder(); } else if (FoundFolder.Contains(SetupFolderKeywords[0])) { CopyDeleteFolder(); //unneccessary second pop up appears due to line 100 } } public void CopyDeleteFolder() { DirectoryInfo GameDirectory = new DirectoryInfo(Game.SelectedPath); string GameContentsPath = Game.SelectedPath + @"\\" + GameDirectory.Name; //inside of the game folder string[] roots = Directory.GetDirectories(Game.SelectedPath, "*", SearchOption.TopDirectoryOnly); foreach (var item in roots) { string? FoundFolder = Path.GetFullPath(item); Directory.Move(FoundFolder, Path.GetFullPath(Setup.SelectedPath) + @"\\" + Path.GetFileName(FoundFolder)); if (Directory.Exists(GameContentsPath)) { string[] GameFiles = Directory.GetFiles(GameContentsPath); foreach (string GameFile in GameFiles) { FileInfo CopyGameFile = new FileInfo(GameFile); if (new FileInfo(GameDirectory + @"\\" + CopyGameFile.Name).Exists == false) { CopyGameFile.MoveTo(GameDirectory + @"\\" + CopyGameFile.Name); } } Directory.Delete(GameContentsPath); } } }
问题分析与修复方案
你猜的没错,问题核心就在foreach var item in roots里的roots变量。
问题根源
- 预加载的目录数组失效:外层循环的
roots是一开始获取的静态目录数组,当你在CopyDeleteFolder里移动/删除目录后,数组里的部分路径已经不存在,但外层循环还会继续尝试访问这些无效路径。 - 重复遍历导致无效操作:每次调用
CopyDeleteFolder都会重新遍历所有目录,同一个文件夹可能被多次处理,甚至尝试操作已经被移动/删除的目录。
修复步骤
- 针对单个目标文件夹处理:外层循环找到符合关键字的文件夹后,直接处理该文件夹,不要调用遍历所有目录的方法。
- 动态获取目录列表:使用
Directory.EnumerateDirectories代替Directory.GetDirectories,避免预加载失效路径。 - 简化路径拼接:用
Path.Combine代替手动拼接路径,减少分隔符错误。
修改后的代码示例
string[] SetupFolderKeywords = { "Setup", "Installed" }; // 动态枚举目录,避免预加载失效路径 foreach (var item in Directory.EnumerateDirectories(Game.SelectedPath, "*", SearchOption.TopDirectoryOnly)) { string foundFolder = Path.GetFullPath(item); // 简化关键字判断逻辑 if (SetupFolderKeywords.Any(keyword => foundFolder.Contains(keyword))) { ProcessSingleFolder(foundFolder); } } public void ProcessSingleFolder(string targetFolder) { DirectoryInfo gameDirectory = new DirectoryInfo(Game.SelectedPath); string gameContentsPath = Path.Combine(gameDirectory.FullName, gameDirectory.Name); // 移动目标文件夹到指定位置 string destPath = Path.Combine(Path.GetFullPath(Setup.SelectedPath), Path.GetFileName(targetFolder)); Directory.Move(targetFolder, destPath); // 处理内容移动与文件夹删除 if (Directory.Exists(gameContentsPath)) { foreach (string gameFile in Directory.GetFiles(gameContentsPath)) { FileInfo fileInfo = new FileInfo(gameFile); string targetFilePath = Path.Combine(gameDirectory.FullName, fileInfo.Name); if (!File.Exists(targetFilePath)) { fileInfo.MoveTo(targetFilePath); } } Directory.Delete(gameContentsPath); } }
内容的提问来源于stack exchange,提问作者Ahmed Sherif
相关产品推荐
相关产品推荐

