0

我的代码:

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

4

1 回答 1

0

原因似乎是它是一个目录。在本文的例外情况下:http: //msdn.microsoft.com/en-us/library/system.io.file.delete.aspx它说:

The caller does not have the required permission.
-or-
path is a directory.
-or-
path specified a read-only file.

注意:路径是一个目录。您可能正在寻找的是:http: //msdn.microsoft.com/en-us/library/fxeahc5f.aspx Directory.Delete(...

/翻转

于 2013-05-24T14:23:05.030 回答