代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
彻底删除文件
作者:
欣玉
/ 发布于
2015/11/4
/
600
经常用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 }); } } }
试试其它关键字
彻底删除文件
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
欣玉
贡献的其它代码
(
17
)
.
定时清空文件内容,定时记录文件大小
.
JAVA对象转换为JSON及日期格式转换处理
.
分割字符串返回表
.
精确测量一个方法执行所花费的时间
.
创建SQLSERVER MD5()
.
获取当天或某个日期是星期几
.
可重复使用的存储过程(增,删,改,查,分页)
.
批量从本地上传文件到指定服务器或从指定服务器下载文
.
中英文排序算法
.
List的Find方法的使用
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3