C#winform:删除列表中的临时文件 [英] C# winform : delete temp file in the list show
本文介绍了C#winform:删除列表中的临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我实际上如何使用C#删除临时文件?我已经使用(System.IO.Path.GetTempPath)获取临时文件列表,代码如下所示。那么我如何使用C#删除文件?
我尝试过:
How actually i can delete the temporary file using C# ?. I already get the list of temporary file using (System.IO.Path.GetTempPath) and the code is shown below. So how do i need to delete the file using C# ?
What I have tried:
if(checkBox2.Checked){
DirectoryInfo Dir = new DirectoryInfo(System.IO.Path.GetTempPath());
FileInfo[] Files = Dir.GetFiles();
foreach (FileInfo file in Files)
{
listView1.Items.Add(file.Name);
File.Delete(file.Name);
}
}
推荐答案
<pre>
static void Main(string[] args)
{
List<string> listView1 = new List<string>();
DirectoryInfo Dir = new DirectoryInfo(System.IO.Path.GetTempPath());
FileInfo[] Files = Dir.GetFiles();
int faildelcount = 0;
foreach (FileInfo file in Files)
{
try
{
File.Delete(file.Name);
}
catch (Exception ex)
{
faildelcount++;
}
}
Console.WriteLine("Temp files cleared successfully");
if(faildelcount > 0)
Console.WriteLine("Unable to delete {0} files", faildelcount.ToString());
Console.ReadLine();
}
尝试:
File.Delete(file.Name);
string tempfolder = ("C:\\Users\\Administrator\\AppData\\Local\\Temp");
string[] tempfiles = Directory.GetFiles(tempfolder, "*.*", SearchOption.AllDirectories);
foreach (string filePath in Directory.GetFiles(tempfolder, "*.*", SearchOption.AllDirectories))
{
try
{
FileInfo currentFile = new FileInfo(filePath);
currentFile.Delete();
}
catch (Exception ex)
{
Debug.WriteLine("Error on file: {0}\r\n {1}", filePath, ex.Message);
}
}
这篇关于C#winform:删除列表中的临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文