专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

C#实现彻底删除文件的方法

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 1:02:07       共计:3603 浏览

本文实例讲述了C#实现彻底删除文件的方法。分享给大家供大家参考。具体实现方法如下:

经常用360的文件粉碎,删除隐私文件貌似还不错的。不过C#也可以实现彻底删除文件。试了下用360文件恢复恢复不了源文件了。

主要代码如下:

public class AbsoluteFile 

       public event EventHandler FinishDeleteFileEvent = null; 
       public event EventHandler FinishDeleteFolderEvent = null; 
       public event EventHandler DeleteErrorEvent = null; 
       public string ErrorString = string.Empty; 
       public void DoAbsoluteDeleteFile(object filePath) 
       { 
           try 
           { 
               string filename = filePath.ToString(); 
               if (string.IsNullOrEmpty(filename)) 
               { 
                   return; 
               } 
               if (File.Exists(filename)) 
               { 
                   File.SetAttributes(filename, FileAttributes.Normal); 
                   double sectors = Math.Ceiling(new FileInfo(filename).Length / 512.0); 
                   byte[] dummyBuffer = new byte[512]; 
                   RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); 
                   FileStream inputStream = new FileStream(filename, FileMode.Open); 
                   inputStream.Position = 0; 
                   for (int sectorsWritten = 0; sectorsWritten < sectors; sectorsWritten++) 
                   { 
                       rng.GetBytes(dummyBuffer); 
                       inputStream.Write(dummyBuffer, 0, dummyBuffer.Length); 
                       sectorsWritten++; 
                   } 
                   inputStream.SetLength(0); 
                   inputStream.Close(); 
                   DateTime dt = new DateTime(2049, 1, 1, 0, 0, 0); 
                   File.SetCreationTime(filename, dt); 
                   File.SetLastAccessTime(filename, dt); 
                   File.SetLastWriteTime(filename, dt); 
                   File.Delete(filename); 
                   WipeDone(); 
               } 
           } 
           catch (Exception e) 
           { 
               WipeError(e); 
           } 
       } 
       public void DoDeleteFolder(object folder) 
       { 
           string folderPath = folder.ToString(); 
           if (string.IsNullOrEmpty(folderPath)) 
           { 
               return; 
           } 
           DirectoryInfo direct = new DirectoryInfo(folderPath); 
           FileSystemInfo[] filesystem = direct.GetFileSystemInfos(); 
           if (filesystem == null || filesystem.Length == 0) 
           { 
               direct.Delete(); 
           } 
           else 
           { 
               foreach (FileSystemInfo fileItem in filesystem) 
               { 
                   if (fileItem is FileInfo) 
                   { 
                       this.DoAbsoluteDeleteFile(fileItem.FullName); 
                   } 
                   else 
                   { 
                       DoDeleteFolder(fileItem.FullName); 
                   } 
               } 
           } 
           if (this.FinishDeleteFolderEvent != null) 
           { 
               this.FinishDeleteFolderEvent(this, null); 
           } 
       } 
       private void WipeError(Exception e) 
       { 
           if (DeleteErrorEvent != null) 
           { 
               ErrorString = e.Message; 
               DeleteErrorEvent(this, null); 
           } 
       } 
       private void WipeDone() 
       { 
           if (FinishDeleteFileEvent != null) 
           { 
               FinishDeleteFileEvent(this, null); 
           } 
       } 
}

顺便写了个简单的界面:

后台代码如下所示:

public partial class Form1 : Form 

       private Thread dfileThread = null; 
       private string fileObjectPath = string.Empty; 
       private bool isFile = true; 
       public delegate void DefaulEventDelegate(object sender, EventArgs e); 
       public Form1() 
       { 
           InitializeComponent(); 
       } 
       private void btnSelectFile_Click(object sender, EventArgs e) 
       { 
           isFile = true; 
          this.openFileDialog1 = new OpenFileDialog(); 
          openFileDialog1.Multiselect = true; 
          openFileDialog1.Title = "请选择文件"; 
          openFileDialog1.Filter = "所有文件(*.*)|*.*"; 
          if (openFileDialog1.ShowDialog() == DialogResult.OK) 
           { 
              this.txtFilePath.Text = openFileDialog1.FileName; 
           } 
          fileObjectPath = this.txtFilePath.Text.Trim(); 
       } 
       private void btnSelectFolder_Click(object sender, EventArgs e) 
       { 
           isFile = false; 
           this.folderBrowserDialog1  = new FolderBrowserDialog(); 
           folderBrowserDialog1.Description = "请选择文件路径"; 
           if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) 
           { 
               this.txtFolder.Text = folderBrowserDialog1.SelectedPath; 
           } 
           fileObjectPath = this.txtFolder.Text.Trim(); 
       } 
       private void btnAbsoluteDelete_Click(object sender, EventArgs e) 
       { 
           this.lblHint.Visible = true; 
           if(string.IsNullOrEmpty(fileObjectPath)) 
           { 
               MessageBox.Show("请选择要粉碎的文件和文件夹!"); 
               return; 
           } 
           AbsoluteFile dfile = new AbsoluteFile(); 
           dfile.DeleteErrorEvent += new EventHandler(Dfile_DeleteErrorEvent); 
           if (isFile) 
           { 
               dfile.FinishDeleteFileEvent += new EventHandler(Dfile_FinishDeleteFileEvent); 
               dfileThread = new Thread(new ParameterizedThreadStart(dfile.DoAbsoluteDeleteFile)); 
               dfileThread.IsBackground = true; 
               dfileThread.Start(fileObjectPath); 
           } 
           else 
           { 
               dfile.FinishDeleteFolderEvent += new EventHandler(Dfile_FinishDeleteFolderEvent); 
               dfileThread = new Thread(new ParameterizedThreadStart(dfile.DoDeleteFolder)); 
               dfileThread.IsBackground = true; 
               dfileThread.Start(fileObjectPath); 
           } 
           this.lblHint.Text = @"正在删除..."; 
       } 
       void Dfile_DeleteErrorEvent(object sender, EventArgs e) 
       { 
           if (!this.InvokeRequired) 
           { 
               MessageBox.Show((sender as AbsoluteFile).ErrorString); 
           } 
           else 
           { 
               this.BeginInvoke(new DefaulEventDelegate(Dfile_DeleteErrorEvent), new object[] { sender, e }); 
           } 
       } 
       void Dfile_FinishDeleteFolderEvent(object sender, EventArgs e) 
       { 
           if (!this.InvokeRequired) 
           { 
               this.lblHint.Visible = false; 
               MessageBox.Show("删除完毕!"); 
           } 
           else 
           { 
               this.BeginInvoke(new DefaulEventDelegate(Dfile_FinishDeleteFolderEvent),new object[] { sender,e }); 
           } 
       } 
       void Dfile_FinishDeleteFileEvent(object sender, EventArgs e) 
       { 
           if (!this.InvokeRequired) 
           { 
               this.lblHint.Visible = false; 
               MessageBox.Show("删除完毕!"); 
           } 
           else 
           { 
               this.BeginInvoke(new DefaulEventDelegate(Dfile_FinishDeleteFileEvent), new object[] { sender, e }); 
           } 
       } 
}

完整实例代码点击此处本站下载。

希望本文所述对大家的C#程序设计有所帮助。

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:基于IDEA搭建springboot+redis环境,深入了解相关机制 | ·下一条:C# 删除文件以及文件夹

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有