删除空文件夹并重新创建后,System.IO.DirectoryNotFoundException

我想复制一个文件夹,我想先删除目标文件夹。 所以我删除目标文件夹然后重新创建它然后复制文件。 问题是我在尝试复制文件时遇到An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll 。 这是代码

 static public void CopyFolder(string sourceFolder, string destFolder) { if (Directory.Exists(destFolder)) // check if folde exist { Directory.Delete(destFolder, true); // delete folder } Directory.CreateDirectory(destFolder); // create folder string[] files = Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = Path.GetFileName(file); string dest = Path.Combine(destFolder, name); File.Copy(file, dest, true); FileInfo fileinfo = new FileInfo(dest); // get file attrib if (fileinfo.Attributes != FileAttributes.ReadOnly) // check if read only File.SetAttributes(dest, FileAttributes.Normal); }....... 

我在此行中获得exceptionFileInfo fileinfo = new FileInfo(dest);

似乎文件夹的创建有延迟,同时我尝试将文件复制到其中。 有任何线索,有什么问题? 完整的exception消息:

mscorlib.dll中发生未处理的“System.IO.DirectoryNotFoundException”类型exception

附加信息:找不到路径’C:\ Users \ joe \ Desktop \ destfolder \ .buildpath’的一部分。

正如好人所指出的,这个例外的原因是我尝试在删除过程完成之前重新创建文件夹。 因此解决方案是在删除后添加2行代码: GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers();

所以正确的代码将是

 static public void CopyFolder(string sourceFolder, string destFolder) { if (Directory.Exists(destFolder)) // check if folde exist { Directory.Delete(destFolder, true); // delete folder GC.Collect(); // CODE ADDED GC.WaitForPendingFinalizers(); // CODE ADDED } Directory.CreateDirectory(destFolder); // create folder string[] files = Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = Path.GetFileName(file); string dest = Path.Combine(destFolder, name); File.Copy(file, dest, true); FileInfo fileinfo = new FileInfo(dest); // get file attrib if (fileinfo.Attributes != FileAttributes.ReadOnly) // check if read only File.SetAttributes(dest, FileAttributes.Normal); }....... 

这样,您等待创建,直到删除过程完成。 Yhanks所有人,特别是Saeed。

你有点不对劲。 exception的原因是仍然有一个资源正在访问该文件夹(或文件)。

解决方案永远不是GC.collect()Sleep() ……这只是一个解决方法。 你正在做的只是让垃圾收集器处理资源,然后给它时间采取行动。

正确的方法是管理自己的资源。 不使用您无法控制的静态方法,而是使用using块并在块的末尾处理资源。 这样,在等待不受您控制的事情(GC)时,没有任何开销。

使用控制资源的对象, using块将在最后处置它。

在您的情况下,使用控制该资源的单个DirectoryInfo对象应该工作。

我对你目前的解决方案感到困惑。 GC与删除文件夹无关,它只能起作用,因为添加GC相关function类似于添加Thread.Sleep()或MessageBox。 它只是偶然的。

相反,您应该等到目录实际被删除,例如:

 Directory.Delete(destFolder, true);  // delete folder while(Directory.Exists(destFolder)) { Thread.Sleep(100); } 

只有在此代码完成后,您才能继续。

尝试使用FileIO方法,我有同样的问题,下面的代码工作完美。

FileIO.FileSystem.DeleteDirectory(目录名,FileIO.DeleteDirectoryOption.DeleteAllContent)

复制文件后,为什么要设置文件属性? 这有必要吗?

您可以做的是首先设置不同的属性,然后再进行实际的文件复制。 它也更有意义,首先检查它是否是ReadOnly文件,如果是,则将其设置为Normal,然后进行复制。

很抱歉回答但没有评论,但我的声誉还不高。 在编写MSDN时,路径必须“格式良好” http://msdn.microsoft.com/en-gb/library/system.io.fileinfo%28v=VS.90%29.aspx

也许通过将文件放在工作目录中来尝试一下? 因此,不需要路径,您可以查看问题是在路径中还是在文件中…

好的,这很奇怪。 仅当目标文件夹为空时才会发生exception。 但删除目标文件夹后添加下面的行似乎解决了问题。 行:MessageBox.Show(“文件夹”+ destFolder +“文件夹已删除”,“警告”);

 static public void CopyFolder(string sourceFolder, string destFolder) { if (Directory.Exists(destFolder)) // check if folder exist { Directory.Delete(destFolder, true); // delete folder MessageBox.Show("folder " + destFolder + "folder was deleted", "alert"); } Directory.CreateDirectory(destFolder); // create folder string[] files = Directory.GetFiles(sourceFolder); foreach (string file in files) { string name = Path.GetFileName(file); string dest = Path.Combine(destFolder, name); File.Copy(file, dest, true); FileInfo fileinfo = new FileInfo(dest); // get file attrib if (fileinfo.Attributes != FileAttributes.ReadOnly) // check if read only File.SetAttributes(dest, FileAttributes.Normal); } 

因此在删除后放置MessageBox.show会导致System.IO.DirectoryNotFoundException消失。 就好像删除后有一个小延迟的事实,文件夹的重新创建顺利。 我想我会找到一个解决这个问题的工作,但如果有人知道是什么导致了这个问题以及解决问题的方法,我会很高兴听到它。

尝试首先设置dest的Fileinfo,然后复制

  foreach (string file in files) { string name = Path.GetFileName(file); string dest = Path.Combine(destFolder, name); FileInfo fileinfo = new FileInfo(dest); // get file attrib dest.Attributes = FileAttributes.Normal; File.Copy(file, dest, true); }.......