Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/01/2010, 16:11
Ecj
 
Fecha de Ingreso: octubre-2008
Mensajes: 63
Antigüedad: 15 años, 6 meses
Puntos: 0
De acuerdo Cerrar archivo en uso

Buenas tardes.

Tengo una inquietud estoy usando el siguiente codigo para saber si el archivo esta en uso:

Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Function ArchivoEnUso(ByVal sFileName As String) As Boolean
Dim hFile As Long
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const OPEN_EXISTING = &H3
Const GENERIC_WRITE = &H40000000
Const INVALID_HANDLE_VALUE = -1
On Error GoTo ExitGetFileInfo
hFile = CreateFile(sFileName, GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0&, 0&)
If hFile = INVALID_HANDLE_VALUE Then
ArchivoEnUso = True
End If
ExitGetFileInfo:
hFile = CloseHandle(hFile)
End Function

Private Sub Command1_Click()
MsgBox ArchivoEnUso("c:\archivo.doc")
End Sub

Y no ahi problema pero como haria para que si el archivo esta en uso la aplicacion lo cierre.

Muchas gracias por cualquier colaboracion.