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");
}