2015-07-01 21 views
0

我想模擬一個錯誤,但我很難完成這個。我的下面的源代碼應該查看每個驅動器並找到一個文件夾或文件,並將其作爲參數傳入。然後它將它在驅動器(%SystemDrive%)中找到的內容寫入日誌文件(.log)中。代碼在我的本地機器上工作,但是當其他人試圖運行它時,他們會得到一個UnauthorizedAccessException:訪問路徑'c:\ ARAGORN.log'被拒絕。出於某種奇怪的原因,該人無法將結果寫入該日誌文件,我不明白爲什麼。有人可以看到,如果可以模擬此異常。請從命令行或PowerShell運行。需要幫助模擬C#中的異常嗎?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Management; 
using System.Security.Permissions; 
using System.Collections; 

/*** 
*                       
* Written by:                       
*                       
* Description: Look through every single drive to find files with the name <the file that you are look of> (Specify name in the string theString). 
*    All files are then stored in an ArrayList called fileList. 
* 
* Notes: If a file does note have the exact name, it will be ignored. 
*   This only finds files with the name you are looking for. Directories will be ignored. 
*                       
* 
* */ 
namespace FileFinder 
{ 
    class Program 
    { 
     static ArrayList fileList = new ArrayList(); 
     static ArrayList folderList = new ArrayList(); 

     static void Main(string[] args) 
     { 
      //Console.WriteLine("There are this many args: " + args.Length); 
      if(args.Length < 1) 
      { 
       Console.WriteLine("Too few arguments"); 
       System.Environment.Exit(1); 
      } 
      else if(args.Length > 1) 
      { 
       Console.WriteLine("Too many arguments"); 
       System.Environment.Exit(1); 
      } 
      //change the name you search for a specific file name 
      //Console.WriteLine("Enter the file name"); 
      string hostName = System.Net.Dns.GetHostName(); 
      string theString = args[0]; 
      foreach (DriveInfo d in DriveInfo.GetDrives()) 
      { 
       string drive = d.Name; 
       try 
       { 
        Console.WriteLine("Looking in " + drive + " for " + theString); 
        LookForFileInDir(drive, theString); 
       } 
       catch (Exception e) 
       { 
        Console.WriteLine(e.Message); 
       } 


      } 
      //Console.WriteLine("The Host Name is: " + hostName); 
      Console.WriteLine("There are " + fileList.Count + " files that were found with the name " + theString); 
      Console.WriteLine("There are " + folderList.Count + " folders that were found with the name " + theString); 



      var actualPath = Environment.ExpandEnvironmentVariables(@"%SystemDrive%\"+hostName+".log"); 
      using (System.IO.StreamWriter file = new System.IO.StreamWriter(actualPath)) 
      { 
       file.WriteLine("bytes\t" + "file"); 
       foreach (string f in fileList) 
       { 
        file.WriteLine(f); 
       } 
       file.WriteLine(); 
       file.WriteLine("\t" + "folder"); 
       foreach (string folder in folderList) 
       { 
        file.WriteLine(folder); 
       } 
      } 
      Console.WriteLine("Press enter to close..."); 
      Console.ReadLine(); 
     } 

     /** 
     * 
     * Looks for files in directories 
     * 
     * */ 
     static void LookForFileInDir(string folder, string theString) 
     { 
      string output = ""; 
      //Console.WriteLine("I am not losing my mind"); 
      foreach (string file in Directory.GetFiles(folder, "*" + theString + "*.*")) 
      { 
       //Console.WriteLine("I am not losing my mind"); 
       if (!IsLink(file)) 
       { 
        FileInfo info = new FileInfo(file); 
        Console.WriteLine(info.Length + "\t" + file); 
        fileList.Add(info.Length + "\t" + file); 
        output += file; 
       } 
      } 

      foreach (string subDir in Directory.GetDirectories(folder, theString)) 
      { 
       try 
       { 
        Console.WriteLine("\t" + subDir); 
        folderList.Add("\t" + subDir); 
       } 
       //Ignores all shortcuts in a drive 
       catch (UnauthorizedAccessException e) 
       { 

       } 
       catch (Exception e) 
       { 
        Console.WriteLine(e.Message); 
        output += e.Message; 
       } 
      } 


      foreach (string subDir in Directory.GetDirectories(folder)) 
      { 
       try 
       { 
        LookForFileInDir(subDir, theString); 

       } 
       //Ignores all shortcuts in a drive 
       catch (UnauthorizedAccessException e) 
       { 

       } 
       catch (Exception e) 
       { 
        Console.WriteLine(e.Message); 
        output += e.Message; 
       } 
      } 

     } 
     /// <summary> 
     /// Returns whether the given path/file is a link 
     /// </summary> 
     /// <param name="shortcutFilename"></param> 
     /// <returns></returns> 
     public static bool IsLink(string shortcutFilename) 
     { 
      string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename); 
      string filenameOnly = System.IO.Path.GetFileName(shortcutFilename); 

      Shell32.Shell shell = new Shell32.Shell(); 
      Shell32.Folder folder = shell.NameSpace(pathOnly); 
      Shell32.FolderItem folderItem = folder.ParseName(filenameOnly); 
      if (folderItem != null) 
      { 
       return folderItem.IsLink; 
      } 
      return false; // not found 
     } 

    } 
} 
+5

您是否嘗試過使用管理員權限執行程序?或者將日誌文件放在Temp文件夾而不是root C: – Piters

+0

文件的讀/寫權限是什麼?你能完全控制每個人嗎? – user1666620

+0

我對@Piters所說的感覺很好。很多時候,如果您具有讀取權限但不能寫入權限,則可能發生這種情況。在我無法寫入的地方爲日誌文件創建子目錄時遇到了這個特殊問題。 –

回答

0

默認情況下,命令提示符不會以管理員權限運行。在Windows Vista或更高版本上,C驅動器被鎖定而沒有管理員權限(或不禁用UAC和一些安全性)被寫入。

我建議改變你的日誌文件的位置。桌面將是一個好地方,您可以使用特殊文件夾將其放置在任何用戶的桌面上。

Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 

其他文件夾的選擇是available from MSDN,你可能需要,如果你的經理希望在系統文件夾,但最終用戶不能使用提升的權限給予一定的反推。如果可以,請確保它們以管理員身份運行。

我們也可以告訴大家,例外的是文件夾的訪問權限,因爲:

  1. 它不是從擴大環境變量。如果這個失敗,則拋出ArgumentNullException
  2. 異常指向UAC。 MSDN涵蓋了這個異常的原因:

的UnauthorizedAccessException例外,通常由 封裝Windows API調用的方法拋出。要找到例外的原因, 將檢查異常對象的Message屬性的文本。 UnauthorizedAccessException使用具有值0x80070005的HRESULT COR_E_UNAUTHORIZEDACCESS, 。

+0

我不認爲這是一個管理權限問題。我運行它沒有管理員權限,我仍然能夠寫入日誌文件。這個問題是否與這個人在PowerShell中運行它有關。我被告知將它寫入%SystemDrive%,因此我無法更改位置。 – LetUsSeng

+0

錯誤非常明顯 - 它無法訪問該文件。 Windows不喜歡在根驅動器上創建文件。未經授權的訪問意味着它沒有權限執行你所要求的操作。 – oppassum

0

爲了模擬異常剛否認與C您的調試用戶的寫訪問:\

+0

我是Visual Studio的新手,我會如何去做。 – LetUsSeng

+0

要麼在操作系統中執行此操作。或以編程方式:http://stackoverflow.com/questions/11472974/setting-up-filesystem-permissions – Wolf5

0

其實根C:是不是一個好地方,轉儲文件 - 標準的Windows訪問限制是有道理的這種方式。在你的地方,我會考慮移動日誌文件我。即到臨時文件夾(如我的第一條評論所述)。這應該可以解決問題。

但是,如果你堅持讓文件保持原樣,我會確保使用管理員權限執行程序。這也應該解決問題。