奇怪的例外与文件访问和explorator窗口 [英] Strange exception with file access and explorator windows

查看:90
本文介绍了奇怪的例外与文件访问和explorator窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个简单的程序:

private static void Main(string[] args)
{
        var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Directory");

        if (Directory.Exists(directoryName))
            Directory.Delete(directoryName, true);

        Directory.CreateDirectory(directoryName);

        var stream = File.Create(Path.Combine(directoryName, "File")); //throws
        stream.Close();
}

这同时,您只需执行这个程序工作正常。如果您浏览目录 Windows资源管理器,然后运行奇怪的事情发生了。在这种情况下,我得到UnautorizedAccessException 访问路径C:\Users\rfurman\AppData\Roaming\Directory\File被拒绝

This works fine while you simply execute this program. The strange thing happens if you browse that Directory in windows explorer and then run. In this case I get UnautorizedAccessException "Access to the path 'C:\Users\rfurman\AppData\Roaming\Directory\File' is denied."

如果这是奇怪的,然后用相同的条件下执行的:

If this is strange then execute this with the same conditions:

private static void Main(string[] args)
{
        var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Directory");

        if (Directory.Exists(directoryName))
            Directory.Delete(directoryName, true);

        var value = Directory.Exists(directoryName);

        Console.WriteLine(value);
        Console.ReadKey();
 }

这程序打印如果目录在Explorer中打开。

This program prints True if Directory is open in explorer.

我想知道的是为什么发生这种情况以及如何防止这种情况。

What I would like to know is why this happens and how to defend against such situation.

我用Windows 7和.NET 4。

I use windows 7 and .net 4.

推荐答案

Directory.Delete 内部使用 RemoveDirectory 的Kernel32 赢得API。什么RemoveDirectory确实是标记为删除目录。当该目录的最后一个句柄被关闭,删除目录。我认为,这意味着后,探险家留下的文件夹

Directory.Delete internally uses RemoveDirectory win api in Kernel32. What RemoveDirectory does is "to mark directory for deletion". Directory is deleted when last handle of that directory is closed. I believe this means "after explorer left that folder"

在我的电脑上这种情况不会发生,所以我无法测试,但我怀疑有可能是你的一种方式。基于NT内核的系统有时允许文件和目录的重命名,即使他们是开放的。我不知道这是允许的确切情况,但我用这个重命名加载的DLL文件,并写入新的是这样的:

In my computer this situation does not occur, so I cannot test but I suspect there may be a way for you. NT based systems sometimes allows renaming of files and directories even if they are open. I don't know exact cases this is allowed, but I used this to rename loaded dll files and write new ones like this:

File.Rename(@"C:\App\test.dll", @"C:\App\test.dll");
File.Copy(@"C:\App\Update\test.dll-v1.1", @"C:\App\test.dll");



所以,你的代码可能看起来像这样改变之后

So your code may look like this after change

var directoryName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Directory");

if (Directory.Exists(directoryName)) {
    var randomExt = ".random"; // generate randomly
    Directory.Move(directoryName, directoryName + randomExt)
    Directory.Delete(directoryName + randomExt, true);
}
Directory.CreateDirectory(directoryName);

var stream = File.Create(Path.Combine(directoryName, "File")); //throws
stream.Close();

这篇关于奇怪的例外与文件访问和explorator窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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