Home > Back-end >  How to monitor a disk? (once detected file is written to, immediately to shear the files to another
How to monitor a disk? (once detected file is written to, immediately to shear the files to another

Time:10-05

Now need to implement such a function as described in the topic, but his knowledge shallow, consult what you need to use the API? How to achieve?

CodePudding user response:

Can search FindFirstChangeNotification functions, there are still some examples, such as
http://bbs.csdn.net/topics/280032788

CodePudding user response:

This is the code for the BCB, as for copying files with CopyFileW or the MoveFile function can

 
A, in. H file add the following code:

//the system time -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#include
Private://User declarations
HANDLE hChange;
Int counter;
Public://User declarations

Two, in a. CPP file add the following code:

__fastcall TForm1: : TForm1 (TComponent * Owner)
: TForm (the Owner)
{
//the default button state
For the - & gt; Enabled=true;
Button2 - & gt; Enabled=false;
//shut down timer
Timer1 - & gt; Enabled=false;
Timer1 - & gt; The Interval=100;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Void __fastcall TForm1: : Button1Click (TObject * Sender)
{
//start monitoring C: \ temp directory file move
HChange=: : FindFirstChangeNotification (" C: \ \ temp, "
True,//include subdirectories
FILE_NOTIFY_CHANGE_FILE_NAME);
//若此目录不存在
If (hChange==INVALID_HANDLE_VALUE) {
ListBox1 - & gt; The Items - & gt; Add (" C: \ temp directory does not exist or could not be opened!" );
//send off information to end the program
Application - & gt; The Terminate ();
} else {
//switch button state
For the - & gt; Enabled=false;
Button2 - & gt; Enabled=true;
//record start time
Struct time st.
Gettime (& amp; St);
ListBox1 - & gt; The Items - & gt; Add (" start monitoring C: \ \ temp directory file move: "+
AnsiString (st. ti_hour) + "_" +
AnsiString (st. ti_min) + "_" +
AnsiString (st. ti_sec)
);

//start the timer, the calculated value file changes XianGui zero
Counter=0;
//start the timer
Timer1 - & gt; Enabled=true;
}
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Void __fastcall TForm1: : Button2Click (TObject * Sender)
{
//release handle
: : FindCloseChangeNotification (hChange);
//switch button state
For the - & gt; Enabled=true;
Button2 - & gt; Enabled=false;
//the default timer state
Timer1 - & gt; Enabled=false;
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Void __fastcall TForm1: : Timer1Timer (TObject * Sender)
{
//if the thread change, back to the information
DWORD rc=: : WaitForSingleObject (hChange, 0);
If (rc==WAIT_OBJECT_0) {
ListBox1 - & gt; The Items - & gt; Add (" common "+ AnsiString (+ + counter) +" file set up or change or delete ");
//remove notification now and wait for the next change notification
: : FindNextChangeNotification (hChange);
}
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Void __fastcall TForm1: : FormDestroy (TObject * Sender)
{
//prevention unreleased directory handle
//: : FindCloseChangeNotification (hChange);
}

CodePudding user response:

This is BCB6, XE wide character types such as String into a String

CodePudding user response:

Can use Microsoft undisclosed API: SHChangeNotifyRegister register a notice, and then in WM_SHNOTIFY message processing line,

This kind of implementation method without the timer,

CodePudding user response:

To learn,

CodePudding user response:

With the clock also can yet be regarded as a solution

CodePudding user response:

To learn, this is used to copy someone else's U disk?
  • Related