C # Delete the $ RECYCLE. BIN file on the mobile hard disk and create an index file

Source: Internet
Author: User
Tags createindex


 

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.