离奇的目录中删除的SSD驱动器上的行为 [英] Bizarre directory delete behaviour on SSD drive

查看:159
本文介绍了离奇的目录中删除的SSD驱动器上的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目录c:\测试中有50个左右的文件,没有子目录

Directory c:\test has 50 or so files in it, no subdirectories.

    If IO.Directory.Exists("C:\test") Then
        IO.Directory.Delete("C:\test", True)
    End If

    IO.Directory.CreateDirectory("C:\test")

驱动器C是Intel的X25-M80 SSD硬盘,操作系统为Windows 7 64位与TRIM支持,Visual Studio是2008年目标框架3.5。当执行上面的code,CreateDirectory打破没有(看到)的异常code执行。经过多次头痛,我发现删除尚未完成的时候code执行到达CreateDirectory。如果我修改我的code是这样的:

Drive C is Intel's X25-M80 SSD drive, OS is Windows 7 64 bit with TRIM support, Visual Studio is 2008 with target framework 3.5. When above code is executed, CreateDirectory breaks code execution without an (visible) exception. After much headache I found that Delete is not yet done by the time code execution gets to CreateDirectory. If I modify my code like this:

    If IO.Directory.Exists("C:\test") Then
        IO.Directory.Delete("C:\test", True)
    End If
    Threading.Thread.Sleep(2000)
    IO.Directory.CreateDirectory("C:\test")

然后一切按预期工作。

then everything works as expected.

在这里,我明显的跆拳道旁边的问题是:

My questions beside the obvious WTF here are:

  • 在不应该IO.Directory.Delete是一个阻塞函数调用无论驱动器是什么
  • 是删除SSD作弊由于启用了TRIM支持?

推荐答案

我有问题,这之前,但这并不是专门针对SSD驱动器。你会好得多做一个举动,然后删除:

I've had problems with this before, but this is not specific to SSD drives. You would be far better off doing a move then delete:

if(Directory.Exists(dirpath))
{
    string temppath = dirpath + ".deleted";
    Directory.Move(dirpath, temppath);
    Directory.Delete(temppath, true);
}
Directory.Create(dirpath);

另一种方式来解决它是进行循环,直至完成:

The other way to deal with it is to loop until complete:

if(Directory.Exists(dirpath))
{
    Directory.Delete(dirpath, true);
    int limit = 100;
    while(Directory.Exists(dirpath) && limit-- > 0)
        Thread.Sleep(0);
}
Directory.Create(dirpath);

这篇关于离奇的目录中删除的SSD驱动器上的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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