Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Saber si el archivo esta siendo usado

1,394 views
Skip to first unread message

Daniel

unread,
Oct 26, 2002, 11:51:30 AM10/26/02
to
Que tal grupo.
Debe ser el día o que estoy trabajando el sábado, pero
alguien sería tan amable de decirme como hago para saber
que un archivo está siendo utilizado por alguien?.

Gracias por la ayuda.
Saludos
Danieñl

Softjaen

unread,
Oct 26, 2002, 1:24:53 PM10/26/02
to

"Daniel" escribió:

> Debe ser el día o que estoy trabajando el sábado, pero
> alguien sería tan amable de decirme como hago para saber
> que un archivo está siendo utilizado por alguien?.

Hola, Daniel:

Prueba abrir el archivo en modo de escritura mediante la función API
«CreateFile». Si se produce un error, es porque el archivo está abierto. Te
dejo una función que te devolverá True/False dependiendo si el archivo está
en uso o no. Sólo tienes que pasarle la ruta del archivo que deseas
verificar:

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

' Obtenemos el manipulador del archivo. Para ello indicamos
' que vamos a permitir el acceso de lectura
hFile = CreateFile(sFileName, GENERIC_WRITE, FILE_SHARE_READ Or
FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0&, 0&)

' Si hay un error es porque el archivo está siendo utilizado
If hFile = INVALID_HANDLE_VALUE Then
ArchivoEnUso = True
End If

ExitGetFileInfo:
' Cerramos el manipulador del archivo
hFile = CloseHandle(hFile)
End Function

Un saludo

--
Enrique Martínez
[MS MVP - VB]

Nota: Este mensaje, así como el código incluido en él, se proporciona «como
está», sin garantías de ninguna clase, y no otorga ningún derecho
.


Rubén Vigón

unread,
Oct 28, 2002, 3:12:19 AM10/28/02
to
Private Function ArchivoEstaAbierto(ByVal Ruta As String) As Boolean
On Error GoTo HayErrores
Dim f As Integer
f = FreeFile
Open Ruta For Append As f
Close f
ArchivoEstaAbierto = False
Exit Function
HayErrores:
If Err.Number = 70 Then
ArchivoEstaAbierto = True
Else
Err.Raise Err.Number
End If
End Function

Un saludo!

Rubén Vigón
Maicrosoft LVP / MOP Certified
Departamento de informática, Grupo Sodes S.A.
www.sodes.com


Daniel

unread,
Oct 28, 2002, 7:39:22 AM10/28/02
to
Gracias muchachos. Lo voy a probar.
Saludos
Daniel


>-----Mensaje original-----

>.
>

0 new messages