删除一个空文件夹并重新创建它后System.IO.DirectoryNotFoundException [英] System.IO.DirectoryNotFoundException after deleting an empty folder and recreating it

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

问题描述

我想一个文件夹复制,和我想先删除目标文件夹。
,所以我删除目标文件夹,然后重新创建它,然后复制文件。
的问题是,我得到试图复制文件时,
中的类型System.IO.DirectoryNotFoundExceptionmscorlib.dll中发生未处理的异常。
这是代码

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.dll" when 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的=新的FileInfo(DEST);

i get the exception in this line "FileInfo fileinfo = new FileInfo(dest);".

好像有是创建的文件夹,并在平均时间延迟我尝试将文件复制到它。
任何线索,什么porblem

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 porblem

完整的异常消息:


未处理的异常'System.IO.DirectoryNotFoundException'
出现在mscorlib.dll

An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll

更多信息:找不到$路径
'C:\Users\joe\Desktop\destfolder\.buildpath'的b $ BA一部分。

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

,因为它是由优秀的人指出的那样,这个异常的原因是完成删除过程之前,我尝试重新创建文件夹。
这样的解决方法是删除后添加两行代码。
GC.Collect的();

SOLUTION

as it been pointed out by good people, the reason for this exception is that i try recreating the folder before the deletion process is finished. so the solution is to add 2 lines of code after deletion. GC.Collect();

GC.WaitForPendingFinalizer();

所以正确的代码会

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



这样,你就等着与创建,直到删除处理结束。
感谢大家,特别是赛义德。

this way , you wait with the creation until the deletion process is finished. thanks 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的( )睡眠() ...这只是一个变通。
你在做什么只是让资源的垃圾收集处置,然后给它的时间采取行动。

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.

从右办法就是管理自己的资源。相反,你有没有控制对一个静态方法的,使用使用块和块结束资源的处置。这种方式有没有开销,而你等待的事情是不是你的控制(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆