2013-08-07 27 views
2

我正在編寫回歸測試,需要手動將文件從一個位置移動到另一個位置。每次發生UnauthorizedAccessException時,我假設這與文件夾的權限有關,文件必須從哪裏移除?我檢查了文件屬性並且它沒有設置爲只讀。從其他問題和答案中,我試過 將程序中的屬性設置爲「正常」。我也想過使用SetAccessControl會有所幫助,但我無法確定如何設置FileSecurity參數。當然,我也可以解決這個問題。 在權限方面,我是本地機器和網絡上的管理員,如果我嘗試從PowerShell中移動文件到相應位置以及從中移出文件,我不會遇到任何問題,我甚至不必提升或強制,Visual Studio運行在不同的權限上,如果是的話,我該如何改變它? 下面是代碼:當試圖移動文件時收到UnauthorizedAccessException

internal static bool Process5010Claims(string batch) 
    { 
     string batchRegex = createBatchRegex(batch); 
     string batchOnFileSystem = addDecimalToBatch(batch); 
     bool isFound = false; 
     string pth = @"\\hedgefrog\root\uploads"; 
     string destination = @"\\apexdata\data\claimstaker\claims\auto\5010"; 
     string[] files = Directory.GetFiles(pth); 
     foreach (var file in files) 
     { 
      if (Regex.IsMatch(file, batchRegex)) 
      { 
       string fullPath = Path.Combine(pth, batchOnFileSystem); 
       var attr = new FileInfo(fullPath); 


       // 
       try 
       { 
        File.Move(fullPath, destination); 
        isFound = true; 
        break; 
       } 
       catch (FileNotFoundException) 
       {//Already been moved to the new directory 
       } 
       catch (UnauthorizedAccessException e) 
       { 
        //In the middle of being moved? 
       } 
       catch (IOException) 
       { 
       }//Already been moved to the new directory 
      } 
     } 

異常沒有給我任何真實的信息,所有我得到的是: UnauthorizedAccessException被抓 查看在被拒絕

回答

2

這樣看來,路徑您在進行移動時未指定文件的名稱。

嘗試更改代碼這樣:

if (Regex.IsMatch(file, batchRegex)) 
     { 
      var fullPath = Path.Combine(pth, batchOnFileSystem); 
      var fullDestinationPath = Path.Combine(destination, batchOnFileSystem); 
      var attr = new FileInfo(fullPath); 
      try 
      { 
       File.Move(fullPath, fullDestinationPath); 
       isFound = true; 
       break; 
      } 
相關問題