嘿傢伙,所以我在一個程序上工作,它刪除了某些目錄文件,主要是臨時文件,除了我得到一個錯誤,甚至知道我添加了一個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