c#删除移动硬盘中$RECYCLE.BIN的文件、建立索引文件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace 为硬盘文件建立索引
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// summary
/// 文件路径
/// /summary
string fpath = "";
/// summary
/// 初始化路径
/// /summary
/// param name="rootDirectory" /param
private void BeginWrite(string rootDirectory)
{
fpath = @rootDirectory + "index.txt";
}
/// summary
/// 写一行数据
/// /summary
/// param name="Text" /param
private void WriteText(string Text)
{
File.AppendAllText(fpath, Text + "\r\n");
}
/// summary
/// 创建索引按钮
/// /summary
/// param name="sender" /param
/// param name="e" /param
private void btnCreateIndex_Click(object sender, EventArgs e)
{
DriveInfo[] DriveInfos = DriveInfo.GetDrives();
foreach (DriveInfo di in DriveInfos)
{
if (di.DriveType == DriveType.Fixed)
{
string rootDirectory = di.RootDirectory.FullName;
///textBoxRoot.Text输入的盘符(如:H)
if (rootDirectory.StartsWith(textBoxRoot.Text.Trim().ToUpper()))
{
lbIndex.Visible = true;
btnCreateIndex.Enabled = false;
BeginWrite(rootDirectory);
Thread th = RunNew(CreateIndex, rootDirectory);
th.Join();//主线程等待线程th运行完毕
// CreateIndex(rootDirectory);
btnCreateIndex.Enabled = true;
lbIndex.Visible = false;
MessageBox.Show("成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
/// summary
/// 创建索引函数
/// /summary
/// param name="PareDirectory" /param
private void CreateIndex(object PareDirectory)
{
string[] Files = Directory.GetFiles(PareDirectory.ToString());//文件列表
string[] DirectoryInfos = Directory.GetDirectories(PareDirectory.ToString());//文件夹列表
int DirectoryCount = DirectoryInfos.Length;//子文件夹个数
for (int k = 0; k Files.Length; k++)
{
try
{
if (Files[k].Contains("$RECYCLE.BIN")) File.Delete(Files[k]);
else WriteText(Files[k]);
}
catch
{
continue;
}
}
for (int i = 0; i DirectoryCount; i++)
{
try
{
if ((new DirectoryInfo(DirectoryInfos[i]).Attributes FileAttributes.Hidden) == FileAttributes.Hidden)
{
continue;
}
CreateIndex(DirectoryInfos[i]);
}
catch
{
continue;
}
}
}
/// summary
/// 创建新线程
/// /summary
/// param name="vfunc" /param
/// param name="para" /param
/// returns /returns
private Thread RunNew(myFunction vfunc, object para)
{
Thread th = new Thread(new ParameterizedThreadStart(vfunc));
th.Start(para);
return th;
}
private delegate void myFunction(object obj);
/// summary
/// 窗体加载
/// /summary
/// param name="sender" /param
/// param name="e" /param
private void Form1_Load(object sender, EventArgs e)
{
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
lbIndex.Visible = false;
}
}
}
我用这个方法把自己的移动硬盘上的$RECYCLE.BIN中的文件删除成功,然后还建议了索引文件。
如下:
本人邮箱:pukuimin@qq.com
相关文章
- C#-CHTTPDownload
- C#定位文件位置并修改字节数据
- C#使用ICSharpCode.SharpZipLib压缩后进行web批量下载文件
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
- Word控件Spire.Doc 【加密解密】教程(一):在 C#、VB.NET 中使用自定义密码加密、解密、保护 Word
- c#+JS原生上传图片、文件支持多文件上传
- c# HttpClient 上传文件并带参
- C#【文件操作篇】复制文件到另一个文件,并显示复制进度
- C#,回文分割问题(Palindrome Partitioning Problem)算法与源代码
- C#,求最长回文字符串的马拉车(Manacher)算法的源代码
- C#读取CAD文件DXF
- c# Bitmap byte[] Stream 文件相互转换
- 在C#中实现Socket端口复用
- C# xml文件的创建,修改和添加节点 。
- C#调用C++的dll文件方法
- C# 卡片式导航
- C#可以做什么
- 浅谈C# 多态的魅力(虚方法,抽象,接口实现)
- C#中Dictionary的用法及用途
- C#ref和out
- 【Unity】C#+Visual Studio生成DLL文件(步骤图解)
- c#上传大文件方法
- git for c#,文件改动内容
- C# 调用API接口处理公共类 自带JSON实体互转类
- C#删除文件和文件夹到回收站
- [转]C# 中的.pdb/ .vshost.exe/ .vshost.exe.manifest文件讨论
- C#-IO-正由另一进程使用,因此该进程无法访问该文件
- C#获取文件版本、文件大小等信息
- C#模拟登录后请求查询