C# 正在尝试删除ProgramData中的文件。即使作为管理员,访问也被拒绝

C# 正在尝试删除ProgramData中的文件。即使作为管理员,访问也被拒绝,c#,windows,file,permissions,C#,Windows,File,Permissions,我的代码: 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 a

我的代码:

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确实存在

原因似乎是它是一个目录。 在本条的例外情况下:它说:

注意:路径是一个目录。 您可能正在寻找的是: 目录。删除(


/Flipbed

文件是否正在被其他进程使用?
The caller does not have the required permission.
-or-
path is a directory.
-or-
path specified a read-only file.