嘿家伙,所以我在一个程序上工作,它删除了某些目录文件,主要是临时文件,除了我得到一个错误,甚至知道我添加了一个catch块。 System.UnauthorizedAccessException。在捕捉IOException异常我到达那里的错误:C#访问路径被拒绝
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);
}
}
而下面这个人是我如何调用该方法:
if (checkBox1.Checked)
{
DeleteInternetFiles(@"C:\Users\" + Environment.UserName + @" \AppData\Local\Microsoft\Windows\Temporary Internet Files");
}
此外,DeleteInternetFiles方法中的参数称为字符串internetDirectory,某些原因无法发布它。 – DialUp
第二个catch块只捕获原始try中代码中的UnauthorizedAccessException,而不是从第一个catch块中获取。 – BurningLights