étecter l'ajout ou la modification d'un fichier sur le disque.

ygr2 Messages postés 2 Date d'inscription mardi 24 janvier 2006 Statut Membre Dernière intervention 2 février 2006 - 31 janv. 2006 à 11:47
ygr2 Messages postés 2 Date d'inscription mardi 24 janvier 2006 Statut Membre Dernière intervention 2 février 2006 - 2 févr. 2006 à 13:56
Bonjour,
je cherche à coder une application qui permet de détecter l'ajout ou la modification d'un fichier sur le disque.
Je ne souhaitre pas utiliser un timer mais plutot reagir à un évènement (j'ai vu qu il existait la fonction BINDEVENT mais je n'ai pas trouve d exemples).
Merci de votre aide

2 réponses

Mike Gagnon Messages postés 381 Date d'inscription vendredi 15 octobre 2004 Statut Membre Dernière intervention 24 octobre 2013 2
2 févr. 2006 à 12:45
Il n'y pas vraiment de timer, mais il faut que tu fasse une surveillance d'une maniere ou d'une autre. L'exemple qui suit surveille la création de fichier et non la création de dossier.


PUBLIC frm
frm = CreateObject ("Tform", SYS(2023)) && surveiller le dossier c:\TEMP
frm.Visible = .T.


DEFINE CLASS Tform As Form
#DEFINE FILE_NOTIFY_CHANGE_FILE_NAME 1
#DEFINE FILE_NOTIFY_CHANGE_DIR_NAME 2
#DEFINE FILE_NOTIFY_CHANGE_ATTRIBUTES 4
#DEFINE FILE_NOTIFY_CHANGE_SIZE 8
#DEFINE FILE_NOTIFY_CHANGE_LAST_WRITE 16
#DEFINE FILE_NOTIFY_CHANGE_LAST_ACCESS 32
#DEFINE FILE_NOTIFY_CHANGE_CREATION 64
#DEFINE FILE_NOTIFY_CHANGE_SECURITY 128
#DEFINE INVALID_HANDLE_VALUE -1
#DEFINE WAIT_OBJECT_0 0
#DEFINE watchingInterval 1000 && milliseconds


Width = 400
Height = 150
MaxButton = .F.
BorderStyle = 2
Caption = "Surveiller le répertoir"


hNotify = INVALID_HANDLE_VALUE
PathBeingWatched = ""

ADD OBJECT lblTarget As Label WITH;
Left=10, Top=7, Autosize=.T.


ADD OBJECT lblAlert As Label WITH;
Left=10, Top=30, Autosize=.T., Caption="Notification:"


ADD OBJECT tm As Timer WITH Interval = 0


PROCEDURE Load
THIS.decl


FUNCTION Init (lcPath)
THIS.PathBeingWatched = FULLPATH (lcPath)


IF Not THIS.startWatching()
= MESSAGEB ("Notification handle error.")
RETURN .F.
ENDIF


PROTECTED FUNCTION startWatching
LOCAL lResult


* no subdirs watched
THIS.hNotify = FindFirstChangeNotification (;
THIS.PathBeingWatched, 0,;
FILE_NOTIFY_CHANGE_FILE_NAME +;
FILE_NOTIFY_CHANGE_LAST_WRITE )


lResult = (THIS.hNotify <> INVALID_HANDLE_VALUE)


IF lResult
THIS.lblTarget.Caption = "Surveillé: " +;
THIS.PathBeingWatched
THIS.tm.Interval = watchingInterval
ENDIF
RETURN lResult


PROTECTED PROCEDURE continueWatching
IF FindNextChangeNotification (THIS.hNotify) = 0
THIS.stopWatching
= MESSAGEB ("Request error.")
THIS.Release
ENDIF
THIS.tm.Interval = watchingInterval


PROTECTED FUNCTION stopWatching
THIS.tm.Interval = 0
IF THIS.hNotify <> INVALID_HANDLE_VALUE
= FindCloseChangeNotification (THIS.hNotify)
ENDIF


FUNCTION _signaled && return signaled state
RETURN (WaitForSingleObject(THIS.hNotify, 0) = WAIT_OBJECT_0)


PROCEDURE _notify && notify on event
THIS.lblAlert.Caption = "Notification: " + TTOC(DATETIME())
THIS.continueWatching


PROCEDURE tm.Timer
IF ThisForm._signaled()
ThisForm._notify
ENDIF


PROCEDURE Destroy
THIS.stopWatching


PROTECTED PROCEDURE decl
DECLARE INTEGER FindFirstChangeNotification IN kernel32;
STRING lpPathName, INTEGER bWatchSubtree, INTEGER dwNotifyFilter


DECLARE SHORT FindNextChangeNotification IN kernel32 INTEGER hChangeHandle
DECLARE SHORT FindCloseChangeNotification IN kernel32 INTEGER hChangeHandle


DECLARE INTEGER WaitForSingleObject IN kernel32;
INTEGER hHandle, INTEGER dwMilliseconds
ENDDEFINE




Mike Gagnon
0
ygr2 Messages postés 2 Date d'inscription mardi 24 janvier 2006 Statut Membre Dernière intervention 2 février 2006
2 févr. 2006 à 13:56
Il existe pourtant un évènement Windows déclanché lors de la modification d'un répertoire, mais, à priori, il n'est pas récupérable par FoxPro, en tout cas je n'ai pas trouvé d'exemple. Je pense donc qu'on est donc obligé de "surveiller" le répertoire.
Merci
0
Rejoignez-nous