数据粉粹

  1. public class AbsoluteFile  
  2.    {  
  3.        public event EventHandler FinishDeleteFileEvent = null;  
  4.   
  5.        public event EventHandler FinishDeleteFolderEvent = null;  
  6.   
  7.        public event EventHandler DeleteErrorEvent = null;  
  8.   
  9.        public string ErrorString = string.Empty;  
  10.   
  11.        public void DoAbsoluteDeleteFile(object filePath)  
  12.        {  
  13.            try  
  14.            {  
  15.                string filename = filePath.ToString();  
  16.   
  17.                if (string.IsNullOrEmpty(filename))  
  18.                {  
  19.                    return;  
  20.                }  
  21.   
  22.                if (File.Exists(filename))  
  23.                {  
  24.                    File.SetAttributes(filename, FileAttributes.Normal);  
  25.                     
  26.                    double sectors = Math.Ceiling(new FileInfo(filename).Length / 512.0);  
  27.                     
  28.                    byte[] dummyBuffer = new byte[512];  
  29.                     
  30.                    RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();  
  31.                     
  32.                    FileStream inputStream = new FileStream(filename, FileMode.Open);  
  33.   
  34.                    inputStream.Position = 0;  
  35.                     
  36.                    for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++)  
  37.                    {  
  38.                        rng.GetBytes(dummyBuffer);  
  39.   
  40.                        inputStream.Write(dummyBuffer, 0, dummyBuffer.Length);  
  41.   
  42.                        sectorsWritten++;  
  43.                    }  
  44.   
  45.                    inputStream.SetLength(0);  
  46.   
  47.                    inputStream.Close();  
  48.                     
  49.                    DateTime dt = new DateTime(2049, 1, 1, 0, 0, 0);  
  50.   
  51.                    File.SetCreationTime(filename, dt);  
  52.   
  53.                    File.SetLastAccessTime(filename, dt);  
  54.   
  55.                    File.SetLastWriteTime(filename, dt);  
  56.   
  57.                    File.Delete(filename);  
  58.   
  59.                    WipeDone();  
  60.                }  
  61.            }  
  62.            catch (Exception e)  
  63.            {  
  64.                WipeError(e);  
  65.            }  
  66.        }  
  67.   
  68.        public void DoDeleteFolder(object folder)  
  69.        {  
  70.            string folderPath = folder.ToString();  
  71.   
  72.            if (string.IsNullOrEmpty(folderPath))  
  73.            {  
  74.                return;  
  75.            }  
  76.   
  77.            DirectoryInfo direct = new DirectoryInfo(folderPath);  
  78.   
  79.            FileSystemInfo[] filesystem = direct.GetFileSystemInfos();  
  80.   
  81.            if (filesystem == null || filesystem.Length == 0)  
  82.            {  
  83.                direct.Delete();  
  84.            }  
  85.            else  
  86.            {  
  87.                foreach (FileSystemInfo fileItem in filesystem)  
  88.                {  
  89.                    if (fileItem is FileInfo)  
  90.                    {  
  91.                        this.DoAbsoluteDeleteFile(fileItem.FullName);  
  92.                    }  
  93.                    else  
  94.                    {  
  95.                        DoDeleteFolder(fileItem.FullName);  
  96.                    }  
  97.                }  
  98.            }  
  99.   
  100.            if (this.FinishDeleteFolderEvent != null)  
  101.            {  
  102.                this.FinishDeleteFolderEvent(this, null);  
  103.            }  
  104.        }  
  105.   
  106.        private void WipeError(Exception e)  
  107.        {  
  108.            if (DeleteErrorEvent != null)  
  109.            {  
  110.                ErrorString = e.Message;  
  111.   
  112.                DeleteErrorEvent(this, null);  
  113.            }  
  114.        }  
  115.   
  116.        private void WipeDone()  
  117.        {  
  118.            if (FinishDeleteFileEvent != null)  
  119.            {  
  120.                FinishDeleteFileEvent(this, null);  
  121.            }  
  122.        }  
  123.    }  


顺便写了个简单的界面:
 
 后台:

 

 

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
 
    1. public partial class Form1 : Form  
    2.    {  
    3.        private Thread dfileThread = null;  
    4.   
    5.        private string fileObjectPath = string.Empty;  
    6.   
    7.        private bool isFile = true;  
    8.   
    9.        public delegate void DefaulEventDelegate(object sender, EventArgs e);  
    10.   
    11.        public Form1()  
    12.        {  
    13.            InitializeComponent();  
    14.        }  
    15.   
    16.        private void btnSelectFile_Click(object sender, EventArgs e)  
    17.        {  
    18.            isFile = true;  
    19.   
    20.           this.openFileDialog1 = new OpenFileDialog();  
    21.   
    22.           openFileDialog1.Multiselect = true;  
    23.   
    24.           openFileDialog1.Title = "请选择文件";  
    25.   
    26.           openFileDialog1.Filter = "所有文件(*.*)|*.*";  
    27.   
    28.           if (openFileDialog1.ShowDialog() == DialogResult.OK)  
    29.            {  
    30.               this.txtFilePath.Text = openFileDialog1.FileName;  
    31.            }  
    32.   
    33.           fileObjectPath = this.txtFilePath.Text.Trim();  
    34.        }  
    35.   
    36.        private void btnSelectFolder_Click(object sender, EventArgs e)  
    37.        {  
    38.            isFile = false;  
    39.   
    40.            this.folderBrowserDialog1  = new FolderBrowserDialog();  
    41.   
    42.            folderBrowserDialog1.Description = "请选择文件路径";  
    43.   
    44.            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)  
    45.            {  
    46.                this.txtFolder.Text = folderBrowserDialog1.SelectedPath;  
    47.            }  
    48.   
    49.            fileObjectPath = this.txtFolder.Text.Trim();  
    50.        }  
    51.   
    52.        private void btnAbsoluteDelete_Click(object sender, EventArgs e)  
    53.        {  
    54.            this.lblHint.Visible = true;  
    55.   
    56.            if(string.IsNullOrEmpty(fileObjectPath))  
    57.            {  
    58.                MessageBox.Show("请选择要粉碎的文件和文件夹!");  
    59.   
    60.                return;  
    61.            }  
    62.   
    63.            AbsoluteFile dfile = new AbsoluteFile();  
    64.   
    65.            dfile.DeleteErrorEvent += new EventHandler(Dfile_DeleteErrorEvent);  
    66.   
    67.            if (isFile)  
    68.            {  
    69.                dfile.FinishDeleteFileEvent += new EventHandler(Dfile_FinishDeleteFileEvent);  
    70.   
    71.                dfileThread = new Thread(new ParameterizedThreadStart(dfile.DoAbsoluteDeleteFile));  
    72.   
    73.                dfileThread.IsBackground = true;  
    74.   
    75.                dfileThread.Start(fileObjectPath);  
    76.            }  
    77.            else  
    78.            {  
    79.                dfile.FinishDeleteFolderEvent += new EventHandler(Dfile_FinishDeleteFolderEvent);  
    80.   
    81.                dfileThread = new Thread(new ParameterizedThreadStart(dfile.DoDeleteFolder));  
    82.   
    83.                dfileThread.IsBackground = true;  
    84.   
    85.                dfileThread.Start(fileObjectPath);  
    86.            }  
    87.   
    88.            this.lblHint.Text = @"正在删除...";  
    89.        }  
    90.   
    91.        void Dfile_DeleteErrorEvent(object sender, EventArgs e)  
    92.        {  
    93.            if (!this.InvokeRequired)  
    94.            {  
    95.                MessageBox.Show((sender as AbsoluteFile).ErrorString);  
    96.            }  
    97.            else  
    98.            {  
    99.                this.BeginInvoke(new DefaulEventDelegate(Dfile_DeleteErrorEvent), new object[] { sender, e });  
    100.   
    101.            }  
    102.        }  
    103.   
    104.        void Dfile_FinishDeleteFolderEvent(object sender, EventArgs e)  
    105.        {  
    106.            if (!this.InvokeRequired)  
    107.            {  
    108.                this.lblHint.Visible = false;  
    109.   
    110.                MessageBox.Show("删除完毕!");  
    111.            }  
    112.            else  
    113.            {  
    114.                this.BeginInvoke(new DefaulEventDelegate(Dfile_FinishDeleteFolderEvent),new object[] { sender,e });  
    115.                  
    116.            }  
    117.        }  
    118.   
    119.        void Dfile_FinishDeleteFileEvent(object sender, EventArgs e)  
    120.        {  
    121.            if (!this.InvokeRequired)  
    122.            {  
    123.                this.lblHint.Visible = false;  
    124.   
    125.                MessageBox.Show("删除完毕!");  
    126.            }  
    127.            else  
    128.            {  
    129.                this.BeginInvoke(new DefaulEventDelegate(Dfile_FinishDeleteFileEvent), new object[] { sender, e });  
    130.   
    131.            }  
    132.        }  
    133.    }  
posted @ 2015-08-20 08:39  applekingghfhfhbr  阅读(372)  评论(1编辑  收藏  举报