我正在尝试模拟错误但我无法完成此操作。我的源代码应该查看每个驱动器,并找到一个文件夹或文件与您作为参数传入。然后它在日志文件(.log)中将它找到的内容写入驱动器(%SystemDrive%)。代码在我的本地计算机上运行,但是当其他人尝试运行它时,它们会收到UnauthorizedAccessException:访问路径&c; \:ARAGORN.log'被拒绝。由于一些奇怪的原因,该人无法将结果写入该日志文件,我不明白为什么。有人可以看看是否可以模拟此异常。请从命令行或powershell运行它。
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
}
}
}
答案 0 :(得分:0)
默认情况下,命令提示符不会以管理员权限运行。在Windows Vista或更高版本上,C驱动器被锁定而无需管理员权限(或者不禁用UAC和某些安全性)。
我建议您更改日志文件的位置。桌面将是一个好地方,您可以使用特殊文件夹将其放置给任何用户。
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
其他文件夹选项为available from MSDN,如果您的经理需要系统文件夹但最终用户无法使用提升的权限,则可能需要提供一些回复。如果可以,请确保他们以管理员身份运行。
我们还可以告诉异常是文件夹访问权限,因为:
ArgumentNullException
。UnauthorizedAccessException异常通常由一个方法抛出 包装Windows API调用。要找到异常的原因, 检查异常对象的Message属性的文本。 UnauthorizedAccessException使用HRESULT COR_E_UNAUTHORIZEDACCESS, 其值为0x80070005。
答案 1 :(得分:0)
要模拟异常,只需拒绝调试用户对C:\
的写入权限答案 2 :(得分:0)
实际上root C:不是转储文件的好地方 - 标准的Windows访问限制是有道理的。在你的位置,我会考虑移动日志文件我。即到Temp文件夹(如我的第一条评论中所述)。这应该可以解决问题。
但是如果你坚持保持文件不变,我会确保用管理员权限执行程序。这也应该解决问题。