当前位置:编程学习 > C#/ASP.NET >>

C# 清理Temp文件实例代码

C# 清理Temp文件实例代码如下:

  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. namespace WinFormTemp
  5. {
  6.     public partial class FormTemp : Form
  7.     {
  8.         public FormTemp()
  9.         {
  10.             InitializeComponent();
  11.             this.HelpButton = true;
  12.             this.MaximizeBox = false;
  13.             this.MinimizeBox = false;
  14.             this.AutoSizeMode = AutoSizeMode.GrowAndShrink; // 禁用手动调整大小。
  15.             this.SizeGripStyle = SizeGripStyle.Hide; // 隐藏调整大小手柄。
  16.             this.StartPosition = FormStartPosition.CenterScreen; // 在桌面居中显示。
  17.         }
  18.         protected override void OnLoad(EventArgs e)
  19.         {
  20.             base.OnLoad(e);
  21.             DirectoryInfo dir = new DirectoryInfo(Path.GetTempPath());
  22.             foreach (FileSystemInfo info in dir.GetFileSystemInfos())
  23.             {
  24.                 try
  25.                 {
  26.                     if (info is FileInfo)
  27.                         info.Delete();
  28.                     else
  29.                         (info as DirectoryInfo).Delete(true);
  30.                 }
  31.                 catch
  32.                 {
  33.                     continue;
  34.                 }
  35.             }
  36.             System.Diagnostics.Process.Start(dir.FullName);
  37.         }
  38.         protected override void OnHelpButtonClicked(System.ComponentModel.CancelEventArgs e)
  39.         {
  40.             base.OnHelpButtonClicked(e);
  41.             e.Cancel = true;
  42.             FileInfo info = new FileInfo("Clear.bat");
  43.             if (info.Exists)
  44.                 info.Attributes = FileAttributes.Normal;
  45.             using (StreamWriter sw = info.CreateText())
  46.             {
  47.                 sw.WriteLine("@echo off");
  48.                 sw.Write(@"rd /s /q ""{0}"" & md ""{0}""", Path.GetTempPath());
  49.             }
  50.             System.Diagnostics.Process.Start(info.DirectoryName);
  51.         }
  52.     }
  53. }
补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,