Programming fun: C # completely delete files

Source: Internet
Author: User

It is often used to smash 360 of files, and it seems pretty good to delete private files. However, C # can also completely delete files. I tried to restore the source file with the 360 file.

The Code is as follows:

 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);            }        }    }

By the way, I wrote a simple interface:

Background:

Public partial class Form1: Form {private Thread dfileThread = null; private string fileObjectPath = string. empty; private bool isFile = true; public delegate void encode (object sender, EventArgs e); public Form1 () {InitializeComponent ();} private void btnSelectFile_Click (object sender, EventArgs e) {isFile = true; this. openFileDialog1 = new OpenFileDialog (); openFileDialog1.Multi Select = true; openFileDialog1.Title = "select a file"; openFileDialog1.Filter = "all files (*. *) | *. * "; if (openFileDialog1.ShowDialog () = DialogResult. OK) {this.txt FilePath. text = openFileDialog1.FileName;} fileObjectPath = this.txt FilePath. text. trim ();} private void btnSelectFolder_Click (object sender, EventArgs e) {isFile = false; this. folderBrowserDialog1 = new FolderBrowserDialog (); folderBrowserDialo G1.Description = "select the file path"; if (folderBrowserDialog1.ShowDialog () = DialogResult. OK) {this.txt Folder. text = folderBrowserDialog1.SelectedPath;} fileObjectPath = this.txt Folder. text. trim ();} private void btnAbsoluteDelete_Click (object sender, EventArgs e) {this. lblHint. visible = true; if (string. isNullOrEmpty (fileObjectPath) {MessageBox. show ("select the file and folder to be crushed! "); 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. finishDeleteFolderE Vent + = new EventHandler (Dfile_FinishDeleteFolderEvent); dfileThread = new Thread (new ParameterizedThreadStart (dfile. doDeleteFolder); dfileThread. isBackground = true; dfileThread. start (fileObjectPath);} this. lblHint. text = @ "deleting... ";} 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 ("deleted! ");} 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 ("deleted! ") ;}Else {this. BeginInvoke (new DefaulEventDelegate (Dfile_FinishDeleteFileEvent), new object [] {sender, e });}}}

Download: http://download.csdn.net/detail/yysyangyangyangshan/6860431

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.