C# 清理IE缓存文件实例代码
来源:
发布时间:2010/9/27
浏览次数:1457
C# 清理IE缓存文件实例代码如下:
1.using System;
2.using System.IO;
3.using System.Windows.Forms;
4.using System.Text.RegularExpressions;
5.
6.namespace WinFormTemp
7.{
8. public partial class FormTemp : Form
9. {
10. public FormTemp()
11. {
12. InitializeComponent();
13. this.HelpButton = true;
14. this.MaximizeBox = false;
15. this.MinimizeBox = false;
16. this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // 禁用手动调整大小。
17. this.SizeGripStyle = SizeGripStyle.Hide; // 隐藏调整大小手柄。
18. this.StartPosition = FormStartPosition.CenterScreen; // 在桌面居中显示。
19. }
20.
21. protected override void OnLoad(EventArgs e)
22. {
23. base.OnLoad(e);
24. DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
25. foreach (FileInfo info in dir.GetFiles("*.*", SearchOption.AllDirectories))
26. {
27. if (Regex.IsMatch(info.Extension, @".(dat|ini)", RegexOptions.IgnoreCase)) // 指定不区分大小写的匹配。
28. continue;
29. try
30. {
31. info.Delete();
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(@"del /f /s /q ""{0}""", Environment.GetFolderPath(Environment.SpecialFolder.InternetCache));
52. }
53. System.Diagnostics.Process.Start(info.DirectoryName);
54. }
55. }
56.}