Fórum Erro ao verificar arquivo #325057
10/07/2006
0
Esta função verifica se um determinado arquivo está em uso.
function IsFileInUse(fName : string) : boolean;
var
HFileRes : HFILE;
begin
Result := false;
if not FileExists(fName) then
begin
exit;
end;
HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
begin
CloseHandle(HFileRes);
end;
end;
O problema é q se eu utilizar variáveis não funciona.
Ex:
IsFileInUse(´\\Cpd_1\c\teste\teste.exe´); // assim funciona
arquivo := ´\\Cpd_1\c\teste\teste.exe´;
IsFileInUse(arquivo); // assim não funciona
Alguém teria algum idéia do que pode estar acontecendo ?
Grato pela atenção.
Turbo Drive
Curtir tópico
+ 0Posts
10/07/2006
Aroldo Zanela
Testei aqui e funcionou normalmente:
var Arquivo: string; begin Arquivo := ´C:\TEMP\Hacson\hacson.exe´; if IsFileInUse(Arquivo) then begin ShowMessage(´Arquivo em uso´); end; end;
Gostei + 0
10/07/2006
Turbo Drive
:cry: :cry: :cry: :cry: :cry: :cry:
Gostei + 0
10/07/2006
Fabiano Góes
realmente testei em rede e não funcionou, porem com mais uma linda de codigo funcionou ;
function IsFileInUse(fName : string) : boolean;
var HFileRes : HFILE;
begin
Result := false;
if not FileExists(fName) then
begin
exit;
end;
HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
begin
CloseHandle(HFileRes);
end;
Result := True; // <= estava faltando o retorno True
end;
testei assim:
[b:cff73ec269]if IsFileInUse(´\\servidorxp\dados\teste.txt´) then showmessage(´Arquivo em uso´);[/b:cff73ec269]
teste agora pra ver se funciona.
uma abraço e espero ter ajudado !!!!
Gostei + 0
11/07/2006
Turbo Drive
Gostei + 0
11/07/2006
Kitsystem
Se vc colocar isto no final da funcao ela sempre sera verdadeira e nao tem sentido usar a funcao
Result := True; // <= estava faltando o retorno True
Gostei + 0
11/07/2006
Fabiano Góes
o amigo kitsystem tem razão, realmente o meu código estava errado mesmo.
Corrigindo:
function IsFileInUse(fName : string) : boolean;
var HFileRes : HFILE;
begin
Result := True;
if not FileExists(fName) then
begin
Result:= False;
exit;
end;
HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
begin
Result := False;
CloseHandle(HFileRes);
end;
end;
Galera não estou com o Delphi aqui para testar, se alguem puder testar e avisar se funcionaou não.
[b:a9952bfb71]mais uma vez desculpe a falha !!![/b:a9952bfb71]
a intenção foi das melhores !!!
Gostei + 0
11/07/2006
Leitorbinario
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)