本文介绍了System.IO.DirectoryNotFoundException在删除一个空文件夹并重新创建之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要复制一个文件夹,我想先删除目标文件夹。
所以我删除目标文件夹,然后重新创建它,然后复制文件。
问题是我得到在尝试复制文件时,mscorlib.dll中出现类型为System.IO.DirectoryNotFoundException的未处理的异常。这是代码

I want to copy a folder, and i want to delete destination folder first.So I am deleting destination folder then recreate it and then copy files.The problem is that i get the An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dllwhen trying to copy files. This is the code

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);
        }.......

我在这一行得到例外 FileInfo fileinfo = new FileInfo(dest);

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

It seems like there is a delay in the creation of the folder and in the mean time I try to copy a file into it. Any clue, what is the problem? The full exception message:

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

Additional information: Could not find a part of the path 'C:\Users\joe\Desktop\destfolder\.buildpath'.



解决方案



如前所述好的人,这个例外的原因是我尝试在删除过程完成之前重新创建该文件夹。
所以解决方案是删除后添加2行代码:

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 everyone,特别是Saeed。

This way, you wait with the creation until the deletion process is finished.Yhanks everyone and especially Saeed.

推荐答案

你有点错了。这个例外的原因是还有一个资源正在访问该文件夹(或文件)。

You got it kinda wrong. The reason for the exception is that there's still a resource that is accessing the folder (or file).

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

The solution is never GC.collect() or Sleep()... That's just a work around.What you're doing is just letting the garbage collector dispose of the resource, and then giving it time to act.

RIGHT 使用块,并在块的末尾处理资源。这样,您的等待不受您控制的东西(GC)就没有开销。

The RIGHT way is to manage your own resources. Instead of a static method that you have no control on, use a using block and dispose of the resource in the end of the block. This way there's no overhead while your waiting for things that aren't under your control (GC).

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

Use an object that controls the resources, and the using block will dispose it at the end.

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

In your case, using a single DirectoryInfo object that controls that resource should work.

这篇关于System.IO.DirectoryNotFoundException在删除一个空文件夹并重新创建之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:05