Erro ao verificar arquivo
Bom dia a todos.
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.
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
Curtidas 0
Respostas
Aroldo Zanela
10/07/2006
Colega,
Testei aqui e funcionou normalmente:
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
Turbo Drive
10/07/2006
Caro Aroldo, vc testou com um arquivo sendo executado em outro computador ? No PC local, realmente funciona, o problema está em um exe em outro PC.
:cry: :cry: :cry: :cry: :cry: :cry:
:cry: :cry: :cry: :cry: :cry: :cry:
GOSTEI 0
Fabiano Góes
10/07/2006
amigo Turbo Drive,
realmente testei em rede e não funcionou, porem com mais uma linda de codigo funcionou ;
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 !!!!
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
Turbo Drive
10/07/2006
Obrigado a todos. Deu certo.
GOSTEI 0
Kitsystem
10/07/2006
O Que o Fabiano colocou esta errado!
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
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
Fabiano Góes
10/07/2006
:oops: [color=red:a9952bfb71]Deculpe[/color:a9952bfb71]
o amigo kitsystem tem razão, realmente o meu código estava errado mesmo.
Corrigindo:
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 !!!
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
Leitorbinario
10/07/2006
Alguem tem uma função que verifica se o arquivo tá em uso, mas sem ser um executavel, tipo Documento.doc ?
GOSTEI 0