首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
其他好文
> 详细
数据粉粹
时间:
2015-08-20 09:09:38
阅读:
126
评论:
0
收藏:
0
[点我收藏+]
标签:
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);
}
}
}
顺便写了个简单的界面:
后台:
[csharp]
view plain
copy
print
?
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://www.cnblogs.com/watchfluture/p/4744035.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
分布式事务
2021-07-29
OpenStack云平台命令行登录账户
2021-07-29
getLastRowNum()与getLastCellNum()/getPhysicalNumberOfRows()与getPhysicalNumberOfCells()
2021-07-29
【K8s概念】CSI 卷克隆
2021-07-29
vue3.0使用ant-design-vue进行按需加载原来这么简单
2021-07-29
stack栈
2021-07-29
抽奖动画 - 大转盘抽奖
2021-07-29
PPT写作技巧
2021-07-29
003-核心技术-IO模型-NIO-基于NIO群聊示例
2021-07-29
Bootstrap组件2
2021-07-29
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!