OVH Cloud OVH Cloud

Fichier verrouillé par processus

1 réponse
Avatar
Fab
Bonjour,

Je cherche un moyen sous Visual Basic de tester si un=20
fichier (.txt) est verrouill=E9 par un processus.
Si quelqu'un a une id=E9e, je suis preneur.

Merci d'avance.

1 réponse

Avatar
Zoury
Essai de l'ouvrir ... si un processus l'a ouvert en mode exclusif tu devrais
recevoir une erreur te disant "accès refusé" ou un truc du genre....

Voici un exemple de code montrant comment implémenté une fonction
FileExists(), une fonction FileInUse() et FileLocked()
'***
' Module1
Option Explicit

Private Sub Main()

Dim hFile As Long

hFile = FreeFile

' on ouvre le fichier en mode exclusif
Open "c:file1.txt" For Binary Access Read Lock Read Write As #hFile
' test si le fichier est utilisé
Debug.Print FileInUse("c:file1.txt") ' renvoit True
' test si le fichier est locké
Debug.Print FileLocked("c:file1.txt") ' renvoit True
' on le ferme
Close #hFile

' on ouvre le fichier en mode normal
Open "c:file1.txt" For Binary As #hFile
' test si le fichier est utilisé
Debug.Print FileInUse("c:file1.txt") ' renvoit True
' test si le fichier est locké
Debug.Print FileLocked("c:file1.txt") ' renvoit False
' on le ferme
Close #hFile

End Sub

Private Function FileLocked(ByRef sFilePath As String) As Boolean

Dim hFile As Long

If (Not FileExists(sFilePath)) Then
hFile = FreeFile
On Error Resume Next
Open sFilePath For Binary As #hFile
FileLocked = Err.Number = 70
Close #hFile
Else
Call Err.Raise(53)
End If

End Function

Private Function FileInUse(ByRef sFilePath As String) As Boolean

Dim hFile As Long

If (Not FileExists(sFilePath)) Then
hFile = FreeFile
On Error Resume Next
Open sFilePath For Binary Access Read Lock Read Write As #hFile
FileInUse = Err.Number = 70
Close #hFile
Else
Call Err.Raise(53)
End If

End Function

Private Function FileExists(ByRef sFilePath As String) As Boolean
FileExists = GetAttr(sFilePath) And vbDirectory
End Function
'***


--
Cordialement
Yanick Lefebvre - MVP pour Visual Basic
http://faq.vb.free.fr/?rubrique=0 - http://www.mvps.org/vbnet/
http://www.mentalis.org/agnet/apiguide.shtml - http://www.mztools.com/

"Fab" a écrit dans le message de
news:2927b01c465ae$75e67950$
Bonjour,

Je cherche un moyen sous Visual Basic de tester si un
fichier (.txt) est verrouillé par un processus.
Si quelqu'un a une idée, je suis preneur.

Merci d'avance.