Secure Delete .Net

本文介绍了一个名为SecureDelete的工具,该工具使用多线程技术来快速获取目录及其子目录下的所有文件,并通过调用API获取文件图标和类型描述,进而显示在用户界面上。此外,还实现了文件的安全删除功能。

 

Secure Delete .Net

文章来自这里

SS-2011.01.14-11.31.16.png

 SS-2011.01.14-11.46.22.png
 We can use a Task to get all of the directory details since Directory.GetDirectories can be quite slow when there are lots of directories to retrieve (e.g. C:\Windows) 
minus.gif  Collapse
var task = Task.Factory.StartNew(() =>
{
	List<string> directoryDetails = new List<string>();
	foreach (string directory in Directory.GetDirectories(startPath))
	{
		directoryDetails.Add(directory);
	}
	return directoryDetails;
});

Task failed = task.ContinueWith(t => HandleTaskError(t.Exception),
	TaskContinuationOptions.OnlyOnFaulted);

Task ok = task.ContinueWith(t => 
	PopulateTree(t.Result, startPath, node), TaskContinuationOptions.OnlyOnRanToCompletion);

Loading the files contained within the directory is also contained in a Task

The method GetFilesFromDirectory starts a new task that calls Directory.GetFiles and generates an ExplorerFileInfo instance for each found file.

For each file retrieved, it checks the cache of 'Known File Types' for a file Icon and file type description. If one isn't found, it calls the APISHGetFileInfo to retrieve the information. It then passes a list of ExplorerFileInfo instances to LoadFilesToList 

minus.gif  Collapse
private void LoadFilesToList(List<ExplorerFileInfo> files)
{
	try
	{
		this.filesList.Items.Clear();
		this.filesList.BeginUpdate();

		ListViewItem[] viewItems = new ListViewItem[files.Count];

		for (int i = 0; i < viewItems.Length; ++i)
		{
			ExplorerFileInfo file = files[i];
			string[] items = new string[]{ file.Name, file.FileSize,
				file.FileTypeDescription, file.LastWriteTime.ToString()};

			ListViewItem listItem = new ListViewItem(items, file.Extension);
			listItem.Name = file.FullName;
			listItem.SubItems[1].Tag = file.Length;

			viewItems[i] = listItem;
		}

		this.filesList.Items.AddRange(viewItems);
	}
	finally                
	{
		this.filesList.EndUpdate();
	}            
}

There are a couple of things to note in LoadFilesToList.

  • Use of BeginUpdate and EndUpdate. Really helps with performance, but I don't see enough people using them
  • Building up a list of items and adding to the ListView using AddRange. This is much quicker than adding to the ListView within the loop via the Items.Add method.

Try browsing to a location that has a large number of files in (many thousand) - performance should still be acceptable.

  minus.gif  Collapse
if (Program.AskQuestion(Resources.DeletionPrompt) == DialogResult.Yes)
{
	// Check here to show a message before starting a thread
	if (!FileCleaner.CheckForDeleteProgram())
	{
		Program.ShowError(Resources.DeletionProgramNotFound);
		return;
	}

	List<string> filesToDelete = new List<string>();
	foreach (ListViewItem item in this.selectedFilesList.Items)
	{
		filesToDelete.Add(item.Name);
	}

	progressWindow.Begin(0, filesToDelete.Count);

	// Starts the deletion process on the worker thread and displays the progress
	// of the operation
	DeletionStartArguments arguments = new DeletionStartArguments(filesToDelete, UserOptions.Current.NumberOfPasses);
	this.deletionWorker.RunWorkerAsync(arguments);

	progressWindow.ShowDialog(this);
}

 

using (Process p = new Process())
{
	p.StartInfo.FileName = Resources.DeletionProgram;
	p.StartInfo.Arguments = string.Format("-p {0} -q \"{1}\"", passes, fileName);
	p.StartInfo.CreateNoWindow = true;
	p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
	p.StartInfo.UseShellExecute = false;
	p.StartInfo.RedirectStandardOutput = true;
	p.Start();

	string output = p.StandardOutput.ReadToEnd();
	p.WaitForExit();

	fileDeleted = !File.Exists(fileName);

	if (UserOptions.Current.LogOperations)
	{
		log.Info(output);
		log.Info(string.Format(Resources.FileDeletionStatus, fileName, fileDeleted.ToString()));
	}
}

 

转载于:https://www.cnblogs.com/zenghui/archive/2011/01/20/SecureDelete.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值