Dav*_*bbo 8 c# windows file-io winapi
在Windows 7(或服务器)框中,我们在UNC共享上有一个文件夹(跨机器UNC,而不是 localhost).我们重命名该文件夹,然后检查新文件夹位置是否存在文件.即使它存在,File.Exists也需要将近5秒钟才能返回true.
完整的repro可以在https://github.com/davidebbo/NpmFolderRenameIssue上找到.这是核心代码:
// This file doesn't exist yet
// Note that the presence of this existence check is what triggers the bug below!!
Console.WriteLine("Exists (should be false): " + File.Exists("test/test2/myfile"));
// Create a directory, with a file in it
Directory.CreateDirectory("test/subdir/test");
File.WriteAllText("test/subdir/test/myfile", "Hello");
// Rename the directory
Directory.Move("test/subdir/test", "test/test2");
var start = DateTime.UtcNow;
// List the files at the new location. Here, our file shows up fine
foreach (var path in Directory.GetFiles("test/test2"))
{
Console.WriteLine(path);
}
for (; ; )
{
// Now do a simple existence test. It should also be true, but when
// running on a (cross machine) UNC share, it takes almost 5 seconds to become true!
if (File.Exists("test/test2/myfile")) break;
Console.WriteLine("After {0} milliseconds, test/test2/myfile doesn't show as existing",
(DateTime.UtcNow - start).TotalMilliseconds);
Thread.Sleep(100);
}
Console.WriteLine("After {0} milliseconds, test/test2/myfile correctly shows as existing!",
(DateTime.UtcNow - start).TotalMilliseconds);
Run Code Online (Sandbox Code Playgroud)
因此,似乎初始存在检查会导致存在值被缓存,从而导致这种虚假行为.
问题:对此有何解释?什么是避免它的最佳方法?
注意:在Windows上使用npm(节点包管理器)时,最初会出现此问题.我这里的代码是repro的C#端口.有关原始Node/npm问题,请参阅https://github.com/isaacs/npm/issues/2230.目标是找到解决它的方法.
Tod*_*ter 10
David,重定向器实现了一个否定的"File Not Found"缓存,它可以防止客户端使用未找到文件的请求充斥服务器.默认缓存时间为5秒,但您可以通过将此值设置为0来修改FileNotFoundCacheLifetime注册表值以控制缓存或禁用缓存.
详细信息:http://technet.microsoft.com/en-us/library/ff686200(v = WS.10).aspx
| 归档时间: |
|
| 查看次数: |
1436 次 |
| 最近记录: |