亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

編程樂趣:C#徹底刪除文件

發布時間:2020-07-29 05:46:24 來源:網絡 閱讀:455 作者:yangyoushan 欄目:編程語言

經常用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 });

            }
        }
    }

下載:http://download.csdn.net/detail/yysyangyangyangshan/6860431


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

紫金县| 铜鼓县| 隆回县| 甘孜县| 绥棱县| 合作市| 吉首市| 潮州市| 鸡西市| 会东县| 桐庐县| 城步| 东明县| 奉贤区| 平乡县| 吴川市| 社会| 宜都市| 美姑县| 香格里拉县| 梨树县| 德昌县| 县级市| 长丰县| 阳谷县| 阿拉善左旗| 肇庆市| 福州市| 抚远县| 涞源县| 西乌| 镇远县| 泌阳县| 永嘉县| 平乡县| 屏山县| 黄冈市| 阜南县| 廊坊市| 松滋市| 乐都县|