奇怪的测试方法失败- Windows安全性

本文关键字:Windows 安全性 失败 测试方法 | 更新日期: 2023-09-27 18:14:44

我可能是瞎了,但这让我难倒了。下面的单元测试在我的本地机器上(但不是在构建服务器上)失败,并出现System.UnauthorizedAccessException异常。

    [TestMethod()]
    public void UserPreferences_LocalApplicationDataPath_PathAlwaysExists()
    {
        //setup: get the proposed path and then delete the bottom folder and any existing files
        string logPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
        string actual = System.IO.Path.Combine(logPath, @"Autoscribe'Matrix'Gemini");
        //if the folder is there, first delete it
        if (Directory.Exists(actual))
        {
            //log some information about what is going on
            Console.WriteLine("Attempting to delete " + actual + " as user " + Environment.UserName);
            Directory.Delete(actual, true); // <-THROWS EXCEPTION HERE
            Console.WriteLine("Deleted");
        }
        //action
        actual = UserPreferencesManager.LocalApplicationDataPath;
        //assert that getting the path forces it to be created.
        Assert.IsTrue(Directory.Exists(actual));
    }

报告的环境。UserName值是"拥有"本地AppData文件夹的Windows用户。为什么不能删除文件夹?奇怪的是,在所有运行测试的机器上(所有Windows 7)都没有这个问题。

奇怪的测试方法失败- Windows安全性

您可能无法在本地删除文件夹,因为仍有其他正在运行的应用程序访问其中的文件。也许您打开了记事本来检查此文件夹中的日志文件?

构建服务器没有这些问题,因为没有用户或进程在机器上实际"工作"。