Directory.Exists明智的时间呢? [英] Directory.Exists sensible to time?

查看:145
本文介绍了Directory.Exists明智的时间呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面一段代码在我的应用程序:(!Directory.Exists(mypath中))

 如果
Directory.CreateDirectory(mypath中);

如果我在一个普通的单元测试运行它有时通过,有时没有。该目录是永远存在的(我确信这一点,所以在技术上它永远不会被代码创造)。但每过一段时间 Directory.Exists(mypath中)返回,这使得代码试图创建的文件夹然后我得到一个 UnauthorizedAccessException



在这里有趣的是,如果我穿上了断点 CreateDirectory ,然后将黄色箭头向上回测试,测试的回报真正



这是怎么回事?



mypath中 \\ nameOfLocalMachine\sharedFolder 。份额是可靠的,不断用... .NET 4.0



我刚刚作出了一个小提琴手可模拟3000 sequentials请求。 175失败...都具有相同的消息:




访问路径\\\
ameOfLocalMachine\sharedFolder\randomFileName.json被拒绝



解决方案

这是硬伤在Windows上非常正常。程序打开的句柄在这样一个目录,指定的删除共享的。这允许任何人删除目录,即使程序使用它。该目录将直到手柄被关闭实际上不从文件系统中消失。接下来是试图重现该目录不能工作,它仍然存在。 Windows生成访问被拒绝的错误,与UnauthorizedAccessException你的C#程序报告。



虽然这听起来像一个不起眼的功能,的每个的节目在Windows中执行此操作。每个进程都有一个默认工作目录,Environment.CurrentDirectory的价值。上创建这样一个目录的手柄可确保在程序正在使用它不能消失。还有其它情况下,FileSystemWatcher的将是另一个例子。或程序忙遍历目录。反恶意软件和搜索索引是臭名昭著的难以诊断此类错误的来源。



另外一个标准的危险的多任务操作系统。你不使用文件系统中的唯一的一个。不重复删除和创建相同的目录应该是你的清单上非常高。如果这是绝对必要的话改名的目录首先你删除它之前。你仍然无法删除的重命名的目录,但你不会失败,重新创建它。可以将其删除后,下一次你需要做到这一点。麻烦低得多的赔率呢。由于有更多的时间过去了。


I have the following piece of code in my application:

if (!Directory.Exists(myPath))
    Directory.CreateDirectory(myPath);

If I run it in a regular unit test sometimes it passes, sometimes not. The directory is always there (I made sure of it, so technically it will never be "created" by code). But every once in a while Directory.Exists(myPath) returned false, which makes the code try to create the folder and then I get an UnauthorizedAccessException!

The funny thing here is if I put a breakpoint on the CreateDirectory, and then move the yellow arrow up back to test, the test returns true!

What's going on?

myPath is \\nameOfLocalMachine\sharedFolder. The share is reliable and constantly used... .NET 4.0

I just made a fiddler simulate 3000 sequentials requests. 175 failed... All with the same message:

Access to the path '\nameOfLocalMachine\sharedFolder\randomFileName.json' is denied

解决方案

This mishap is pretty normal on Windows. Programs open a handle on a directory like this and specify delete sharing. Which permits anybody to delete the directory, even though the program is using it. The directory won't actually disappear from the file system until that handle is closed. What follows is that trying to recreate that directory cannot work, it still exists. Windows generates an "access denied" error, reported in your C# program with the UnauthorizedAccessException.

While that sounds like an obscure feature, every program in Windows does this. Every process has a default working directory, the value of Environment.CurrentDirectory. Creating a handle on such a directory ensures that it cannot disappear while the program is using it. There are other cases, FileSystemWatcher would be another example. Or a program busy iterating the directory. Anti-malware and search indexers are notorious for hard to diagnose sources of such errors.

Otherwise a standard hazard of a multi-tasking operating system. You are not the only one using the file system. Not repeatedly deleting and creating the same directory ought to be very high on your list. If this is absolutely necessary then rename the directory first before you delete it. You'd still fail to delete the renamed directory but you won't fail recreating it. You can delete it later, next time you need to do this. Much lower odds for trouble then. Because more time passed.

这篇关于Directory.Exists明智的时间呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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