简体   繁体   English

C#访问路径被拒绝

[英]C# Access to path denied

Hey guys so I'm working on a program it deletes certain directories files, mostly temp files, except I get an error even know I added a catch block. 大家好,所以我正在开发一个程序,该程序会删除某些目录文件,主要是临时文件,但即使我知道添加了catch块,也会出现错误。 The System.UnauthorizedAccessException. System.UnauthorizedAccessException。 on the catch ioexception I get the error there: 在catch ioexception上,我得到那里的错误:

private void DeleteInternetFiles(string internetDirectory)
{
    DirectoryInfo internetTempStorage = new DirectoryInfo(internetDirectory);
    try
    {
        //this will delete files
        foreach (FileInfo getNetFileInfo in internetTempStorage.GetFiles())
        {
            getNetFileInfo.Delete();
        }

        //this will loop through and delete folders
        foreach (DirectoryInfo tempDirectoryInformation in internetTempStorage.GetDirectories())
        {         
            tempDirectoryInformation.Delete();
        }
    }

    //catch io exception and try delete file again
    catch (IOException)
    {
        //delete file in this directory
        File.Delete(internetDirectory);

        //delete folders in this directory
        Directory.Delete(internetDirectory);
    }

    //catch access exception and delete file again
    catch (UnauthorizedAccessException)
    {
        //delete file in this directory
        File.Delete(internetDirectory);

        //delete folders in this directory
        Directory.Delete(internetDirectory);

    }
}

And this one below is how I call the method: 下面是我如何调用该方法的方法:

if (checkBox1.Checked)
{
    DeleteInternetFiles(@"C:\Users\" + Environment.UserName + @" \AppData\Local\Microsoft\Windows\Temporary Internet Files");
}

Your second call to File.Delete(internetDirectory); 您第二次调用File.Delete(internetDirectory); , inside the catch block, seems likely to be the problem. 在catch块内,似乎是问题所在。 The program has already encountered an error while trying to delete the file, and then you tried again. 该程序在尝试删除文件时已经遇到错误,然后再次尝试。 Two things could be happening: 可能发生两件事:

  1. The user account executing the program doesn't have permission to delete files in another user's directory. 执行该程序的用户帐户无权删除另一个用户目录中的文件。

  2. Some file is still in use and therefore can't be deleted (eg currently open in Internet Explorer. 某些文件仍在使用中,因此无法删除(例如,当前在Internet Explorer中打开。

You might want to study the responses in C# - How to Delete temporary internet files . 您可能需要研究C#中的响应-如何删除Internet临时文件 Note the comments about possibly having to "kill IE". 请注意有关可能必须“杀死IE”的注释。

The problem I see here is that the delete action you're performing requires Administrator privileges. 我在这里看到的问题是您正在执行的删除操作需要Administrator权限。

What you can do is try to right click > Run as Administrator the application and then perform the action. 您可以做的是尝试右键单击>以管理员身份运行该应用程序,然后执行操作。

If you want to prompt the user to elevate your application, you can do this. 如果要提示用户提升您的应用程序,则可以执行此操作。

Force application to Run as Administrator [Winforms only] 强制应用程序以管理员身份运行[仅Winforms]

You get this error because the file or folder you attempt to delete have not this access right. 您收到此错误,因为您尝试删除的文件或文件夹没有此访问权限。

It may happen in your case due to some file is being currently in use while you perform a delete operation. 由于执行删除操作时当前正在使用某些文件,因此可能会发生这种情况。

There are more possibilities of file being used because you delete from a folder that windows os uses for the temporary use. 由于您从Windows os临时使用的文件夹中删除文件,因此存在更多使用文件的可能性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM