欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-16 18:35:34
本文实例讲述了c#实现彻底删除文件的方法。分享给大家供大家参考。具体实现方法如下: 经常用360的文件粉碎,删除隐私文件貌似还不错的。不过c#也可以实现彻底删除文件。试了...

本文实例讲述了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); 
           } 
       } 
}

顺便写了个简单的界面:

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

后台代码如下所示:

复制代码 代码如下:
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#程序设计有所帮助。

上一篇:

下一篇: