Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Evitar error de excepción general con ficheros (https://www.clubdelphi.com/foros/showthread.php?t=62310)

Belaix 16-12-2008 14:47:39

Evitar error de excepción general con ficheros
 
Hola

Con esto:

if (FileExists('c:\file.txt') then

Se si el fichero existe ...

Pero como puedo saber si puedo leerlo y no lo está usando otra aplicación?

duilioisola 16-12-2008 14:56:40

Ábrelo dentro de un bloque try...except.

o

Utiliza {I+} ... {I-}
Esto deshabilita las exceptiones y devuelve el resultado en IOResult.
Si lo abres e IOResult=0 entonces tienes permiso para utilizarlo.

Ejemplo del Help de Delphi
Código Delphi [-]
function FileIsThere(FileName: string): Boolean;

{ Boolean function that returns True if the file exists; otherwise,
  it returns False. Closes the file if it exists. }
 var
  F: file;
begin
  {$I-}
  AssignFile(F, FileName);
  FileMode := 0;  {Set file access to read only }
  Reset(F);
  CloseFile(F);
  {$I+}
  FileIsThere := (IOResult = 0) and (FileName <> '');
end;  { FileIsThere }

Belaix 16-12-2008 16:57:53

Joe, acabo de encontrar este código:

Código Delphi [-]
function FileInUse(FileName: string): Boolean;
var hFileRes: HFILE;
begin
  Result := False;
  if not FileExists(FileName) then exit;
  hFileRes := CreateFile(PChar(FileName),
                                    GENERIC_READ or GENERIC_WRITE,
                                    0,
                                    nil,
                                    OPEN_EXISTING,
                                    FILE_ATTRIBUTE_NORMAL,
                                    0);
  Result := (hFileRes = INVALID_HANDLE_VALUE);
  if not Result then 
    CloseHandle(hFileRes);
end;

Pero el tuyo me gusta muchiiiisimo más.

Mil gracias duilioisola!


La franja horaria es GMT +2. Ahora son las 01:14:00.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi