4

在工作中,我们已经从 Windows XP 迁移到 Windows Vista。迁移后,我的一些使用 nUnit 的单元测试开始随机失败,并引发 System.UnauthorizedAccessException。每个失败的测试都涉及将用于测试的文件作为嵌入式资源存储在测试 DLL 中写入当前目录,运行测试,然后快速连续地删除它们,通常是在 setup/teardown 或 fixture setup/teardown 中。我这样做是为了让我的测试与每个开发人员驱动器上运行它们的位置无关,而不用担心相对文件路径。

在对此进行故障排除时,我发现它与文件的创建和删除有关。删除时,每个删除都遵循以下模式:

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 建议的 Process Monitor,发现 Windows Vista SearchIndexer 和 TortoiseSVN 的 TSVNCache 进程在我的代码运行时尝试访问目标文件,正如 Martin 所建议的那样。

再次感谢。

4

3 回答 3

1

在 vista 中,转到 Performace Monitor (controlpanrl->Administrative tools) 并观察应用程序运行时的虚拟字节(内存泄漏)。性能监视器为您提供了有关您要调查的进程的大量详细信息。我怀疑您的应用程序没有释放资源,因为它在文件系统上大量工作。

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.

于 2009-05-05T06:55:03.263 回答
1

Could it be occassionally colliding with a background service, such a file indexing? Try switching off as many of these services as you can.

于 2009-05-05T07:10:45.847 回答
0

你有任何病毒扫描程序吗?尝试禁用它们或从http://www.sysinternals.com使用 ProcMon 观看文件活动

于 2009-05-04T23:29:42.740 回答