2013-05-21 29 views
0

我可能是失明的,但這讓我難堪。以下單元測試在本地機器上失敗(但不在我們的構建服務器上)並導致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)。

+0

討厭去問,但你確定它是空的嗎?如果它不是空的,我很確定你會被阻止。 – Brian

+0

你能夠在你的系統中創建/刪除'手動'相同的文件夾嗎? 它是否要求'管理員'權限? –

+0

該文件夾確實有一個文件,但是Directory.Delete(...,true)應該處理該文件。它在構建服務器上還有一個文件,我沒有這個問題。 – haughtonomous

回答

0

您可能無法在本地刪除該文件夾,因爲您仍然有其他應用程序在其中運行該訪問文件。也許你已經打開了一個記事本來檢查這個文件夾中的日誌文件?

構建服務器沒有這些問題,因爲沒有用戶或進程實際上在機器上「工作」。

+0

好點,但除了運行測試的Vis Studio以外,我沒有其他任何東西可以打開。這是我想到的冷杉,我仔細檢查過。 – haughtonomous