我的代码:
static public void DeleteFiles(string pathName)
{
//expand path if it has variables.
pathName = Environment.ExpandEnvironmentVariables(pathName);
Console.WriteLine("Trying file: " + pathName);
//if the file exists, delete it and say so. Otherwise, say so.
if (File.Exists(pathName))
{
try
{
File.SetAttributes(pathName, FileAttributes.Normal);
File.Delete(pathName);
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
string lineToWrite = pathName + " deleted.\n";
Console.WriteLine(lineToWrite);
logList.Add(lineToWrite);
Console.ResetColor();
}
catch (Exception ex)
{
string lineToWrite = "Something went wrong... \n" + ex;
Console.WriteLine(lineToWrite);
}
}
else
{
string lineToWrite = pathName + " not found.\n";
Console.WriteLine(lineToWrite);
logList.Add(lineToWrite);
}
}
当我运行这段代码时,它说它不存在。
如果我注释掉“IF”和“ELSE”所以它不检查存在,然后我得到这个错误:
System.UnauthorizedAccessException:对路径“C:\ProgramData\pickles”的访问被拒绝。
在我的清单文件中,我有一行:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
但这似乎没有什么区别。我也试过“highestAvailable”。
有谁知道我可以如何改变我现有的功能以实现我的目标?
注意:ProgramData\pickles 确实存在。