Registriert seit: 2. Sep 2004
Ort: Allgäu
246 Beiträge
Delphi 7 Enterprise
|
ReadDirectoryChangesW
14. Mär 2008, 19:06
Hi,
ich möchte in einen MediaPlayer eine Möglichkeit einbauen das dieser eins oder wenn möglich mehrere Ordner automatisch überwacht und in liste hinzufügt oder entfernt was halt grad passiert...
Dazu hab ich 2 ansätze gefunden aber der erste geht einmal und das wars und der zweite geht garnich
kennt sich einer von euch da aus?
Delphi-Quellcode:
type
FileNotifyInformation = Record
NextEntryOffset : DWORD;
Action : DWORD;
FileNameLength : DWORD;
FileName : Array[0..MAX_PATH] Of WCHAR;
end;
PFileNotifyInformation = ^FileNotifyInformation;
//---------------------------------------------------Version 1------------
procedure TcsDirThread.Execute;
var
pBuf : Pointer;
dwBufLen : DWORD;
dwRead : DWORD;
FNI : FileNotifyInformation;
pWork : Pointer;
sFileName : Widestring;
dwWaitStatus : DWORD;
FhFile : THandle;
hNotifity : Cardinal;
FsFileName : String;
ReNode : TTreeNode;
tempX : String;
i : Integer;
begin
Form1.Memo1.Lines.Add('Überwachung gestartet');
FhFile := CreateFile(PChar(Form1.FolderToWatch1), GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_DIRECTORY or FILE_FLAG_BACKUP_SEMANTICS, 0);
Priority := tpLower;
hNotifity := FindFirstChangeNotification(PChar(Form1.FolderToWatch1), //Verzeichnis
True, //unterverzeichnisse überwachen
FILE_NOTIFY_CHANGE_FILE_NAME or
FILE_NOTIFY_CHANGE_LAST_WRITE or
FILE_NOTIFY_CHANGE_SIZE or
FILE_ACTION_ADDED or
FILE_ACTION_REMOVED or
FILE_ACTION_MODIFIED);
if (FhFile = INVALID_HANDLE_VALUE) or (FhFile = 0) then
begin
RaiseLastWin32Error;
Terminate;
end;
if (hNotifity = INVALID_HANDLE_VALUE) then
begin
RaiseLastWin32Error;
Terminate;
end;
dwBufLen := 65536;
pBuf := AllocMem(dwBufLen);
try
while ((FindNextChangeNotification(hNotifity)) and (not terminated)) do
begin
Synchronize(Application.ProcessMessages);
dwWaitStatus := WaitForSingleObject(hNotifity, 1000);
if (dwWaitStatus = WAIT_FAILED) then
begin
RaiseLastWin32Error;
Terminate;
end;
if (dwWaitStatus = WAIT_OBJECT_0) then
begin
ReadDirectoryChangesW(FhFile,pBuf,dwBufLen,true,
FILE_NOTIFY_CHANGE_FILE_NAME or
FILE_NOTIFY_CHANGE_DIR_NAME or
FILE_NOTIFY_CHANGE_ATTRIBUTES or
FILE_NOTIFY_CHANGE_SIZE or
FILE_NOTIFY_CHANGE_LAST_WRITE or
FILE_NOTIFY_CHANGE_CREATION or
FILE_ACTION_ADDED or
FILE_ACTION_REMOVED or
FILE_ACTION_MODIFIED,
@dwRead,nil,nil);
pWork := pBuf;
repeat
StrMove(@FNI,pWork,12);
PChar(pWork) := PChar(pWork)+12;
sFileName := StringOfChar(#00,FNI.FileNameLength);
StrMove(@sFileName[1],pWork,FNI.FileNameLength);
FsFileName := WideCharToString(PWideChar(sFileName));
FsFileName := copy(FsFileName,1,length(FsFileName) shl 1);
//Synchronize(AddFileToList);
PChar(pWork) := PChar(pBuf)+FNI.NextEntryOffset;
if FNI.Action = FILE_ACTION_ADDED then begin
Form1.Memo1.Lines.Add('+ ' + FsFileName);
end;
if FNI.Action = FILE_ACTION_REMOVED then begin
Form1.Memo1.Lines.Add('- ' + FsFileName);
end;
{ FILE_NOTIFY_CHANGE_FILE_NAME or
FILE_NOTIFY_CHANGE_DIR_NAME or
FILE_NOTIFY_CHANGE_ATTRIBUTES or
FILE_NOTIFY_CHANGE_SIZE or
FILE_NOTIFY_CHANGE_LAST_WRITE or
FILE_NOTIFY_CHANGE_LAST_ACCESS or
FILE_NOTIFY_CHANGE_CREATION {or
FILE_NOTIFY_CHANGE_SECURITY}
//break;
until FNI.NextEntryOffset = 0;
end;
end;
{ if (LowerCase(ExtractFileExt(FsFileName)) = '.bmp') then
begin
PostMessage(MainForm.Handle, Verzeichnisaenderung_bmp, 0, ProjektID);
end; }
finally
FreeMem(pBuf,dwBufLen);
end;
end;
//------------------------------------------------------Version2--------------------------
procedure TMonitorThread.Execute;
var
pBuf: Pointer;
dwBufLen: DWORD;
dwRead: DWORD;
FNI: FileNotifyInformation;
pWork: Pointer;
sFileName: Widestring;
FsFileName : String;
i : Integer;
temp : string;
ReNode : TTreeNode;
DirectoryHandle : THandle;
begin
DirectoryHandle := CreateFile( PCHar(Form1.FolderToWatch2), GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_DIRECTORY or FILE_FLAG_BACKUP_SEMANTICS, 0);
Priority := tpLower;
Form1.Memo2.Lines.Add('Überwachung gestartet');
dwBufLen := 65536;
pBuf := AllocMem(dwBufLen);
while not terminated do
begin
ReadDirectoryChangesW(DirectoryHandle, pBuf, dwBufLen, True,
FILE_NOTIFY_CHANGE_LAST_WRITE,
@dwRead, nil, nil);
pWork := pBuf;
repeat
StrMove(@FNI,pWork,12);
PChar(pWork) := PChar(pWork)+12;
sFileName := StringOfChar(#00,FNI.FileNameLength);
StrMove(@sFileName[1],pWork,FNI.FileNameLength);
FsFileName := WideCharToString(PWideChar(sFileName));
FsFileName := copy(FsFileName,1,length(FsFileName) shl 1);
PChar(pWork) := PChar(pBuf)+FNI.NextEntryOffset;
if FNI.Action = FILE_ACTION_ADDED then begin
Form1.Memo2.Lines.Add('+ ' + FsFileName);
end;
if FNI.Action = FILE_ACTION_REMOVED then begin
Form1.Memo2.Lines.Add('- ' + FsFileName);
end;
{ FILE_NOTIFY_CHANGE_FILE_NAME or
FILE_NOTIFY_CHANGE_DIR_NAME or
FILE_NOTIFY_CHANGE_ATTRIBUTES or
FILE_NOTIFY_CHANGE_SIZE or
FILE_NOTIFY_CHANGE_LAST_WRITE or
FILE_NOTIFY_CHANGE_LAST_ACCESS or
FILE_NOTIFY_CHANGE_CREATION {or
FILE_NOTIFY_CHANGE_SECURITY}
//break;
until FNI.NextEntryOffset = 0;
end;
FreeMem(pBuf,dwBufLen);
end;
//-------------------------------------------------------------------------------------------------------------------------------------v
was mach ich falsch und mach ich was falsch?
Thanx
Tom
Tom Just DO it
|