我想模擬一個錯誤,但我很難完成這個。我的下面的源代碼應該查看每個驅動器並找到一個文件夾或文件,並將其作爲參數傳入。然後它將它在驅動器(%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
}
}
}
您是否嘗試過使用管理員權限執行程序?或者將日誌文件放在Temp文件夾而不是root C: – Piters
文件的讀/寫權限是什麼?你能完全控制每個人嗎? – user1666620
我對@Piters所說的感覺很好。很多時候,如果您具有讀取權限但不能寫入權限,則可能發生這種情況。在我無法寫入的地方爲日誌文件創建子目錄時遇到了這個特殊問題。 –