Vista 中使用 .Net 2.0 进行频繁文件 I/O 时出现 UnauthorizedAccessException
在工作中,我们已经从 Windows XP 迁移到 Windows Vista。 迁移后,我的一些使用 nUnit 的单元测试开始随机失败,并抛出 System.UnauthorizedAccessException。 每个失败的测试都涉及将作为嵌入资源存储在测试 DLL 中的测试所用的文件写入到当前目录,运行测试,然后删除它们,通常是在安装/拆卸或夹具安装/拆卸中快速连续地进行。 我这样做是为了让我的测试与运行它们的每个开发人员驱动器上的位置无关,而不用担心相对文件路径。
在排查此问题时,我发现它与文件的创建和删除有关。 删除时,每次删除都遵循以下模式:
if( File.Exists(path) ) { File.Delete(path) }
当我用 try-catch 块和 catch 上的断点包围它时(如果引发异常),该文件已经从磁盘中删除。 对于文件创建失败,通常使用XmlWriter或StreamWriter,分别指定覆盖文件(如果存在)。
奇怪的是,在调查过程中,我创建了这个 C# 程序,它似乎重新创建了异常:
class Program
{
static void Main(string[] args)
{
int i = 0;
try
{
while (true)
{
System.IO.TextWriter writer = new System.IO.StreamWriter("file.txt");
i++;
System.Console.Out.WriteLine(i);
writer.Write(i);
writer.Close();
System.IO.File.Delete("file.txt");
}
}
catch (System.UnauthorizedAccessException ex)
{
System.Console.Out.WriteLine("Boom at: " + i.ToString());
}
}
}
在我们的一台仍然装有 XP 的机器上,它将不断迭代到数十万次,而不会出现异常,直到我杀死它。 在我们的任何 Vista 机器上,它都会在 150 到 500 次迭代之间打印“Boom”。
由于我在工作之外无法访问 Vista 计算机,因此我无法确定这个特殊的“怪癖”是否是由于我的雇主对 Vista 的安全配置或 Vista 本身造成的。
可以说,我很困惑。
编辑:
我要感谢大家的回复。 我使用 Christian 建议的进程监视器,发现 Windows Vista SearchIndexer 和 TortoiseSVN 的 TSVNCache 进程在我的代码运行时尝试访问目标文件,正如 Martin 建议的那样。
再次感谢。
At work, we have migrated from Windows XP to Windows Vista. After the migration, some of my unit tests, using nUnit, started to randomly fail with the System.UnauthorizedAccessException being thrown. The failing tests each involve writing files used for tests stored as embedded resources in the test DLL to the current directory, running the tests, then deleting them, typically in the setup/teardown or the fixture setup/teardown, in rapid succession. I do this so that my tests are agnostic to the location on each developer's drive that they are ran from and not worrying about relative file paths.
While troubleshooting this, I found that it related to the creation and deletion of the files. On deletion, each delete follows the pattern:
if( File.Exists(path) ) { File.Delete(path) }
When I surround this with a try-catch block and breakpoint on the catch (if the exception was thrown), the file would already be deleted from the disk. For the failures of file creation, usually using XmlWriter or StreamWriter, each are specified to overwrite the file if it exists.
Odd thing is, while investigating, I created this C# program that seems to recreate the exception:
class Program
{
static void Main(string[] args)
{
int i = 0;
try
{
while (true)
{
System.IO.TextWriter writer = new System.IO.StreamWriter("file.txt");
i++;
System.Console.Out.WriteLine(i);
writer.Write(i);
writer.Close();
System.IO.File.Delete("file.txt");
}
}
catch (System.UnauthorizedAccessException ex)
{
System.Console.Out.WriteLine("Boom at: " + i.ToString());
}
}
}
On one of our machines that still has XP on it, it will keep iterating into the hundreds of thousands without excepting until I kill it. On any of our Vista machines, it will print "Boom" anywhere between 150 and 500 iterations.
Since I do not have access to a Vista machine outside of work, I can't determine whether this particular 'quirk' is because of my employer's security configuration of Vista or Vista itself.
Suffice to say, I am quite stumped.
EDIT:
I would like to thank everyone for their responses. I used the Process Monitor suggested by Christian and found that the Windows Vista SearchIndexer and TortoiseSVN's TSVNCache processes were trying to access the target file while my code was running, as suggested by Martin.
Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 vista 中,转到性能监视器(controlpanrl->管理工具)并观察应用程序运行时的虚拟字节(内存泄漏)。 性能监视器为您提供了有关您要调查的流程的大量详细信息。 我怀疑您的应用程序没有释放资源,因为它在文件系统上大量工作。
还可以尝试使用性能监视器来尝试查看可能导致问题的其他措施。 如果不做很少的调查,就很难指责其中一项或另一项服务。
In vista, go to Performace Monitor (controlpanrl->Administrative tools) and observe the virtual bytes(for memory leaks) for your application when it is running. Performance monitor gives you a lot of details about the process you want to investigate. I suspect that your application is not releasing resources since it is working heavily on file system.
Also play around with performance monitor to try and see other measures that might have caused your issue. It is difficult to blame one or the other service with out doing little investigation.
它是否偶尔会与后台服务(例如文件索引)发生冲突? 尝试尽可能多地关闭这些服务。
Could it be occassionally colliding with a background service, such a file indexing? Try switching off as many of these services as you can.
你们有病毒扫描程序吗? 尝试禁用它们或使用 ProcMon 从 http://www.sysinternals.com 观看文件活动
Do you have any virus scanners? Try to disable them or watch the file activity with ProcMon from http://www.sysinternals.com