begin process at 2010 02 09 20:22:28
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Système

 > SURVEILLER LES ACTIVITÉS D'UN DOSSIER ET DE SES SOUS-DOSSIERS (FINDFIRSTCHANGENOTIFICATION FINDNEXTCHANGENOTIFICATION + WAITFORMULTIPLEOBJECTS)

SURVEILLER LES ACTIVITÉS D'UN DOSSIER ET DE SES SOUS-DOSSIERS (FINDFIRSTCHANGENOTIFICATION FINDNEXTCHANGENOTIFICATION + WAITFORMULTIPLEOBJECTS)


 Information sur la source

Note :
Aucune note
Catégorie :Système Classé sous :surveiller, activités, changement, fichiers, dossiers Niveau :Débutant Date de création :09/07/2005 Vu / téléchargé :6 284 / 716

Auteur : taye78

Ecrire un message privé
Ce membre participe au partage de revenus publicitaires
Commentaire sur cette source (1)
Ajouter un commentaire et/ou une note


 Description

Très mince exemple des api FindFirstChangeNotification / FindNextChangeNotification qui permettent d'être notifié lors d'un changement dans un dossier et ses sous-dossiers. Tel comme changement de taille, de la dernière date d'accès, changement de nom de fichier, changement de nom de dossier etc...
Je dis mince exemple car on ne récupère même pas quel fichier/dossier est affecté à l'action ni l'action exacte à avoir été éxécuté sur le fichier/dossier (Ceci dit ce n'est pas bien compliqué de faire un tel code (ReadDirectoryChangesW))
Exemple d'utilisation de l'api WaitForMultipleObjects pour être informé d'un changement d'état.
( Utilisation d'un thread pour le "monitoring" )
Code commenté.
Bref, api+compo standard

Source

  • procedure TThreadMonit.Execute;
  • var
  • dwStatus: DWORD; //status renvoyé par WaitForMultipleObjects
  • hChangeHwnd: PWOHandleArray; //array d'handle a passer a WaitForMultipleObjects
  • MonitPath: PChar; //l'emplacement du répertoire
  • begin
  • with Form1 do
  • begin
  • MonitPath := PChar(edtPath.Text); //on cast la string edtPath.Text dans notre pchar MonitPath
  • while not Terminated do //tant que le thread tourne
  • begin
  • hChangeHwnd[0] := FindFirstChangeNotification(MonitPath, True, //on veut être notifié des changements de nom de fichier
  • FILE_NOTIFY_CHANGE_FILE_NAME);//MonitPath: le dossier à surveiller, True: Si on veut surveiller les sous-dossiers, FILE_NOTIFY_*: type d'action à surveiller
  • if hChangeHwnd[0] = INVALID_HANDLE_VALUE then //si le 1º élément du tableau hChangeHwnd est un handle invalide alors on stop tout
  • begin
  • ShowMessage(SysErrorMessage(GetLastError)); //on affiche un dialog avec la description de l'erreur, en récuperant son erreur via GetLastError
  • Exit;
  • end;
  • hChangeHwnd[1] := FindFirstChangeNotification(MonitPath, True, //on veut être notifié des changements de nom de dossier
  • FILE_NOTIFY_CHANGE_DIR_NAME);
  • if hChangeHwnd[1] = INVALID_HANDLE_VALUE then
  • begin
  • ShowMessage(SysErrorMessage(GetLastError));
  • Exit;
  • end;
  • hChangeHwnd[2] := FindFirstChangeNotification(MonitPath, True, //on veut être notifié des changements de date de dernier accès au fichier
  • FILE_NOTIFY_CHANGE_LAST_ACCESS);
  • if hChangeHwnd[2] = INVALID_HANDLE_VALUE then
  • begin
  • ShowMessage(SysErrorMessage(GetLastError));
  • Exit;
  • end;
  • while True do //loop infinie
  • begin
  • dwStatus := WaitForMultipleObjects(3, hChangeHwnd, FALSE, INFINITE); //Etat d'attente sur notre tableau d'handle qui est de 3 éléments, FALSE= on est alerté quand l'un des handles est apellé (TRUE= on est alerté quand tous sont apellés)
  • case dwStatus of
  • WAIT_OBJECT_0: //1er élément du tableau est apellé (1er handle)
  • begin
  • Form1.ListBox1.Items.Add('Changement détecté : ' +
  • ' Action fichier');
  • if not FindNextChangeNotification(hChangeHwnd[0]) then //on veut être notifié du prochain changement, si cette demande échou on sort de la boucle pour que les handles soient libérés et le thread terminé
  • begin
  • ShowMessage(SysErrorMessage(GetLastError));
  • break;
  • end
  • else
  • continue;
  • end; //obj
  • WAIT_OBJECT_0 + 1: //2º élément du tableau est apellé (2ème handle)
  • begin
  • Form1.ListBox1.Items.Add('Changement détecté : ' +
  • ' Action dossier');
  • if not FindNextChangeNotification(hChangeHwnd[1]) then
  • begin
  • ShowMessage(SysErrorMessage(GetLastError));
  • break;
  • end
  • else
  • continue;
  • end; //obj+1
  • WAIT_OBJECT_0 + 2: //3º élément du tableau est apellé (3ème handle)
  • begin
  • Form1.ListBox1.Items.Add('Changement détecté : ' +
  • ' Fichier accédé'); //saffiche aussi quand l'on crée un dossier vu que le dossier est accéder lors de sa création
  • if not FindNextChangeNotification(hChangeHwnd[2]) then
  • begin
  • ShowMessage(SysErrorMessage(GetLastError));
  • break;
  • end
  • else
  • continue;
  • end; //obj+2
  • else
  • continue;
  • end; //case
  • end; //while
  • FindCloseChangeNotification(hChangeHwnd[0]); //on libère nos handles de notifications
  • FindCloseChangeNotification(hChangeHwnd[1]);
  • FindCloseChangeNotification(hChangeHwnd[2]);
  • Terminate; //on termine notre thread
  • end;//while not terminated
  • end; //with
  • end;
procedure TThreadMonit.Execute;
var
  dwStatus: DWORD;    //status renvoyé par WaitForMultipleObjects
  hChangeHwnd: PWOHandleArray; //array d'handle a passer a WaitForMultipleObjects
  MonitPath: PChar;  //l'emplacement du répertoire
begin
with Form1 do
begin
MonitPath := PChar(edtPath.Text);  //on cast la string edtPath.Text dans notre pchar MonitPath
while not Terminated do   //tant que le thread tourne
begin
  hChangeHwnd[0] := FindFirstChangeNotification(MonitPath, True,              //on veut être notifié des changements de nom de fichier
                                                FILE_NOTIFY_CHANGE_FILE_NAME);//MonitPath: le dossier à surveiller, True: Si on veut surveiller les sous-dossiers, FILE_NOTIFY_*: type d'action à surveiller
  if hChangeHwnd[0] = INVALID_HANDLE_VALUE then  //si le 1º élément du tableau hChangeHwnd est un handle invalide alors on stop tout
  begin
    ShowMessage(SysErrorMessage(GetLastError)); //on affiche un dialog avec la description de l'erreur, en récuperant son erreur via GetLastError
    Exit;
  end;

  hChangeHwnd[1] := FindFirstChangeNotification(MonitPath, True,            //on veut être notifié des changements de nom de dossier
                                                FILE_NOTIFY_CHANGE_DIR_NAME);
  if hChangeHwnd[1] = INVALID_HANDLE_VALUE then
  begin
    ShowMessage(SysErrorMessage(GetLastError));
    Exit;
  end;
  hChangeHwnd[2] := FindFirstChangeNotification(MonitPath, True,           //on veut être notifié des changements de date de dernier accès au fichier
                                                FILE_NOTIFY_CHANGE_LAST_ACCESS);
  if hChangeHwnd[2] = INVALID_HANDLE_VALUE then
  begin
    ShowMessage(SysErrorMessage(GetLastError));
    Exit;  
  end;
 
  while True do  //loop infinie
  begin
    dwStatus := WaitForMultipleObjects(3, hChangeHwnd, FALSE, INFINITE);  //Etat d'attente sur notre tableau d'handle qui est de 3 éléments, FALSE= on est alerté quand l'un des handles est apellé (TRUE= on est alerté quand tous sont apellés) 
    case dwStatus of
      WAIT_OBJECT_0: //1er élément du tableau est apellé (1er handle)
      begin
        Form1.ListBox1.Items.Add('Changement détecté : ' +
                                 ' Action fichier');

        if not FindNextChangeNotification(hChangeHwnd[0]) then  //on veut être notifié du prochain changement, si cette demande échou on sort de la boucle pour que les handles soient libérés et le thread terminé
        begin
          ShowMessage(SysErrorMessage(GetLastError));
          break;
        end
        else
          continue;
      end;    //obj

      WAIT_OBJECT_0 + 1: //2º élément du tableau est apellé (2ème handle)
      begin
        Form1.ListBox1.Items.Add('Changement détecté : ' +
                                 ' Action dossier');
        if not FindNextChangeNotification(hChangeHwnd[1]) then
        begin
          ShowMessage(SysErrorMessage(GetLastError));
          break;
        end
        else
          continue;
      end; //obj+1
      WAIT_OBJECT_0 + 2: //3º élément du tableau est apellé (3ème handle)
      begin
        Form1.ListBox1.Items.Add('Changement détecté : ' +
                                 ' Fichier accédé');   //saffiche aussi quand l'on crée un dossier vu que le dossier est accéder lors de sa création
        if not FindNextChangeNotification(hChangeHwnd[2]) then
        begin
          ShowMessage(SysErrorMessage(GetLastError));
          break;
        end
        else
          continue;
      end; //obj+2

  else
    continue;
  end; //case
end; //while
  FindCloseChangeNotification(hChangeHwnd[0]);  //on libère nos handles de notifications
  FindCloseChangeNotification(hChangeHwnd[1]);
  FindCloseChangeNotification(hChangeHwnd[2]);
  Terminate;  //on termine notre thread
end;//while not terminated
end; //with
end;


 Fichier Zip

Les Membres Club peuvent télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !

Télécharger le zip


 Sources du même auteur

Source avec Zip ASTUCE: COMMENT CHANGER LA TAILLE DE NOS LIGNES DANS UN TLIS...
Source avec Zip CRÉATION D'UN POINT DE RESTAURATION (ME/XP)
Source avec Zip BLOQUER L'ACCÈS À UN VOLUME (LECTEUR: DISQUE DUR, DISQUETTE,...
Source avec Zip LISTEUR DES PROCESSUS ET DE SES CHILDS. KILL N'IMPORTE QUEL ...
METTRE (OU RETIRER) LE MONITEUR EN (DE LA) VEILLE

 Sources de la même categorie

Source avec Zip Source avec une capture GLIBWMI VCL COMPONENT LIBRARY 1.6B par Neftali
Source avec Zip Source avec une capture UNITÉ DE SUPPORT VISTA par Bacterius
Source avec Zip Source avec une capture NETTOYEUR AUTOMATIQUE DE VOS DISQUES par diglas
Source avec Zip Source avec une capture LES VALUE'S FADERS par blueperfect
Source avec Zip Source avec une capture COUNTERS, UNITÉ DE CALCUL DE PERFORMANCE par Bacterius

 Sources en rapport avec celle ci

Source avec Zip Source avec une capture CHANGEUR DE FOND D'ÉCRAN par John Dogget
Source avec Zip Source avec une capture NETTOYAGE AUTOMATIQUE DE NOMS DE FICHIERS par John Dogget
Source avec Zip SAVE PROJET (V1.2.0.0) par EricStib
Source avec Zip Source avec une capture UN CRYPTEUR-DÉCRYPTEUR par supersnail
Source avec Zip SURVEILLER VOTRE MESSAGERIE par Michel34

Commentaires et avis

Commentaire de Nono40 le 12/07/2005 23:27:11

Pour surveiller autrement et en obtenant le détail des modifications :
http://nono40.developpez.com/sources/source0045/
( fonctionne sous NT 2000 XP mais pas 95 98 ME )

Sur tous les systèmes et en utilisant la fonction donnée ci-dessus, j'avais fais ceci il y a un moment :
http://nono40.developpez.com/sources/source0017/
On se sert de la date/heure de modification pour trouver lequel des fichiers à été modifié. Ce n'est pas aussi fiable que la méthode donnée en premier.

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

Pack/Archive de fichier [ par Benitora ] Bonjour,voilà, je cherche a utiliser dans mon programme des fichiers "pack", je m'explique :j'ai plusieurs dossiers et sous-dossiers contenant des fic Fonction pour compter les dossiers et les fichiers [ par mattmfi ] BonjourExiste t'il une fonction qui permet de récupérer le nombres de fichiers et de dossier d'un dossier ?merci Liste de fichier (WEB) [ par l0sth34d ] Bonjour! :PJe suis entrain de travailler sur un auto-updater et voici la façon qu'il doit fonctionner... - Creer une liste de fichiers/dossiers qui so scanner et relever les fichiers/dossiers commençant par la lettre R [ par JackNUMBER ] bonjour à tous !étant un bon débutant, j'aimerai savoir comment faire pour lancer une sorte de scann qui va parcourir tous les fichiers et (sous)dossi 1 er demarrage [ par burnouze ] bonjour j ai fait un ptit prg qui cré des 2 dossiers puis a l'iterieur des dossiers il cré des .txt et au tout premier demarrage si les fichiers n'e 1 er demarrage [ par burnouze ] bonjour j ai fait un ptit prg qui cré des 2 dossiers puis a l'iterieur des dossiers il cré des .txt et au tout premier demarrage si les fichiers n'e Envoie des dossiers et fichiers [ par cybersky ] Bonsoir à tous,Voila mon problème : je voudrais savoir comment faie pour envoier les dossiers partager par un serveur TServerSocket en affichant dans Créer des fichiers éxécutables(.exe) à la volé [ par christophedlr ] Bonjour à tous,Je voudrais savoir comment ont fait pour créer des fichiers EXE comme font les programmes d'installation comme Inno Setup.Parce que, j' Aide fichiers delphi [ par felina1 ] Bonjour à tous,Je cumul formation vb6 et delphi. VB6, je m'en sors à peu près, mais delphi....sans commentaires!Connaissez vous un bon site avec des Environnement de développement non sauvegardé [ par vivelesquads ] Bonjour,J'utilise Delphi 7 sous WinXP SP2. J'ai le problème suivant. Quand je quitte un projet son environnement (fichiers ouverts, breakpoints, ..) n


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Février 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,764 sec (4)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales