删除C#.net中的临时文件 [英] Delete the temp files in C#.net

查看:112
本文介绍了删除C#.net中的临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除C#.net中的临时文件我想列出列表框中的所有临时文件,然后删除所有文件

delete the temp files in C#.net i want to list all the temp files in the listbox and then delete all the files

推荐答案




要获得系统上的Temp路径,请使用以下代码:



Hi,

To get Temp path on your system use this code:

var tmpPath = Path.GetTempPath();





获取所有文件目录使用:





To get all files in that directory use:

var files = Directory.GetFiles(tmpPath, "*.*", SearchOption.AllDirectories);





将文件添加到ListBox:



To add files to your ListBox:

listBox1.AddRange(files);

< br $> b $ b

删除文件很简单:



Deleting files is easy:

foreach(var file in files)
{
    if (File.Exists(file))
    {
        File.Delete(file);
    }
}





别忘了添加 System.IO 到源文件顶部的使用子句。



这里有更多信息:

http://msdn.microsoft.com/en-us/ library / system.io.path.gettemppath%28v = vs.100%29.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/system.io.directory .getfiles%28v = vs.110%29.aspx [ ^ ]

http://msdn.microsoft.com/en-u s / library / system.io.file.exists%28v = vs.90%29.aspx [ ^ ]

http://msdn.microsoft.com/en-us/library/system.io .file.delete%28v = vs.90%29.aspx [ ^ ]



干杯!



And don't forget to add System.IO to your using clause at top of the source file.

More informations here:
http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath%28v=vs.100%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.directory.getfiles%28v=vs.110%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.file.exists%28v=vs.90%29.aspx[^]
http://msdn.microsoft.com/en-us/library/system.io.file.delete%28v=vs.90%29.aspx[^]

Cheers!


这个问题解决了这里的问题阅读本讨论



http://forums.asp.net/t/1920308.aspx?How + to + create + a + application + to + delete + the + temp + files + for + any + system + using + C + net + [ ^ ]







这个 [ ^ ]
This is solved Question here Read this Discussion

http://forums.asp.net/t/1920308.aspx?How+to+create+a+application+to+delete+the+temp+files+for+any+system+using+C+net+[^]

and

This[^]


检查stackoverflow [ ^ ]


这篇关于删除C#.net中的临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆