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 creates an index for Hard Disk Files
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
/// <Summary>
/// File path
/// </Summary>
String fpath = "";
/// <Summary>
/// Initialization path
/// </Summary>
/// <Param name = "rootDirectory"> </param>
Private void BeginWrite (string rootDirectory)
{
Fpath = @ rootDirectory + "index.txt ";
}
/// <Summary>
/// Write a row of data
/// </Summary>
/// <Param name = "Text"> </param>
Private void WriteText (string Text)
{
File. AppendAllText (fpath, Text + "\ r \ n ");
}
/// <Summary>
/// Index creation button
/// </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 input drive letter (for example, H)
If (rootDirectory. StartsWith (textBoxRoot. Text. Trim (). ToUpper ()))
{
LbIndex. Visible = true;
BtnCreateIndex. Enabled = false;
BeginWrite (rootDirectory );
Thread th = RunNew (CreateIndex, rootDirectory );
Th. Join (); // The main thread waits until the th thread finishes running.
// CreateIndex (rootDirectory );
BtnCreateIndex. Enabled = true;
LbIndex. Visible = false;
MessageBox. Show ("successful! "," Prompt ", MessageBoxButtons. OK, MessageBoxIcon. Information );
}
}
}
}
/// <Summary>
/// Create an index function
/// </Summary>
/// <Param name = "PareDirectory"> </param>
Private void CreateIndex (object PareDirectory)
{
String [] Files = Directory. GetFiles (PareDirectory. ToString (); // file list
String [] DirectoryInfos = Directory. GetDirectories (PareDirectory. ToString (); // folder list
Int DirectoryCount = DirectoryInfos. Length; // Number of subfolders
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>
/// Create a thread
/// </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>
/// Form Loading
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void Form1_Load (object sender, EventArgs e)
{
System. Windows. Forms. Control. checkforillegalcrossthreadcils = false;
LbIndex. Visible = false;
}
}
}
I used this method to delete the files in $ RECYCLE. BIN on my mobile hard drive successfully, and then suggested the index file.
As follows:
Author: pukuimin1226