我可能是失明的,但這讓我難堪。以下單元測試在本地機器上失敗(但不在我們的構建服務器上)並導致System.UnauthorizedAccessException。奇怪的測試方法失敗 - Windows安全
[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)。
討厭去問,但你確定它是空的嗎?如果它不是空的,我很確定你會被阻止。 – Brian
你能夠在你的系統中創建/刪除'手動'相同的文件夾嗎? 它是否要求'管理員'權限? –
該文件夾確實有一個文件,但是Directory.Delete(...,true)應該處理該文件。它在構建服務器上還有一個文件,我沒有這個問題。 – haughtonomous