我无法删除临时文件夹目录。这是我的代码:
private void button8_Click(object sender, EventArgs e)
{
if(checkBox5.Checked == true)
{
try
{
string fileDirectory = @"C:\Users\Admin\AppData\Local\Temp";
if(Directory.Exists(fileDirectory))
{
Directory.Delete(fileDirectory);
}
}
catch(IOException ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show("System has been cleaned!");
}
}
答案 0 :(得分:3)
您可以使用Path.GetTempPath()
获取当前用户的临时文件夹。
你不应该删除Temp目录本身。最好删除它的文件,跳过你无法删除的所有文件:
System.IO.DirectoryInfo tempDir = new DirectoryInfo(Path.GetTempPath());
foreach (FileInfo file in tempDir.GetFiles())
{
try
{
file.Delete();
}
catch(IOException ex)
{
.....
}
}
另请参阅:"Directory is not empty" error when trying to programmatically delete a folder