0

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. The System.UnauthorizedAccessException. on the catch ioexception I get the error there:

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");
}
sfuqua
  • 5,797
  • 1
  • 32
  • 33
  • Also the parameter in the DeleteInternetFiles method is called string internetDirectory, some reason can't post it. –  Feb 05 '16 at 02:25
  • The second catch block only catches an UnauthorizedAccessException from the code in the original try, not from inside the first catch block. – BurningLights Feb 05 '16 at 02:32

3 Answers3

2

Your second call to File.Delete(internetDirectory);, inside the catch block, seems likely to be the problem. 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 (e.g. currently open in Internet Explorer.

You might want to study the responses in C# - How to Delete temporary internet files. Note the comments about possibly having to "kill IE".

Community
  • 1
  • 1
sfuqua
  • 5,797
  • 1
  • 32
  • 33
0

The problem I see here is that the delete action you're performing requires Administrator privileges.

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]

Community
  • 1
  • 1
DevEstacion
  • 1,947
  • 1
  • 14
  • 28
0

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.