我可能是盲人,但这让我很难过。以下单元测试在我的本地机器上(但不是在我们的构建服务器上)失败,并出现 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));
}
报告的 Environment.UserName 值是“拥有”本地 AppData 文件夹的 Windows 用户。为什么不能删除文件夹?奇怪的是,我在运行测试的所有机器(所有 Windows 7)上都没有这个问题。