C# 清理Temp文件实例代码
来源:
发布时间:2010/9/27
浏览次数:1084
C# 清理Temp文件实例代码如下:
1.using System;
2.using System.IO;
3.using System.Windows.Forms;
4.
5.namespace WinFormTemp
6.{
7. public partial class FormTemp : Form
8. {
9. public FormTemp()
10. {
11. InitializeComponent();
12. this.HelpButton = true;
13. this.MaximizeBox = false;
14. this.MinimizeBox = false;
15. this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // 禁用手动调整大小。
16. this.SizeGripStyle = SizeGripStyle.Hide; // 隐藏调整大小手柄。
17. this.StartPosition = FormStartPosition.CenterScreen; // 在桌面居中显示。
18. }
19.
20. protected override void OnLoad(EventArgs e)
21. {
22. base.OnLoad(e);
23. DirectoryInfo dir = new DirectoryInfo(Path.GetTempPath());
24. foreach (FileSystemInfo info in dir.GetFileSystemInfos())
25. {
26. try
27. {
28. if (info is FileInfo)
29. info.Delete();
30. else
31. (info as DirectoryInfo).Delete(true);
32. }
33. catch
34. {
35. continue;
36. }
37. }
38. System.Diagnostics.Process.Start(dir.FullName);
39. }
40.
41. protected override void OnHelpButtonClicked(System.ComponentModel.CancelEventArgs e)
42. {
43. base.OnHelpButtonClicked(e);
44. e.Cancel = true;
45. FileInfo info = new FileInfo("Clear.bat");
46. if (info.Exists)
47. info.Attributes = FileAttributes.Normal;
48. using (StreamWriter sw = info.CreateText())
49. {
50. sw.WriteLine("@echo off");
51. sw.Write(@"rd /s /q ""{0}"" & md ""{0}""", Path.GetTempPath());
52. }
53. System.Diagnostics.Process.Start(info.DirectoryName);
54. }
55. }
56.}