This is a client server application. I'm creating a update program that will replace a list of exe files, run scripts, and anything else that needs to be updated. This will be installed on the server.
First I need to check if the executable file is opened via a network share. I can do this manually by going into Computer Management then Shared files and Open files. This seems to be the only way to check if the file is open. I tried using R/W to check if the file is opened but this did not work. Looked at Win32_ServerConnection but this just listed number of files that were open not the names.
I would like to code this in Delphi 7 or C# if it can't be done in Delphi. I have found a few programs that can view the open files on a server but nothing on how this can be done.
I use a function like this to check whether I can alter a file on the filesystem. Basically I try to "create" a new file called fName, still opening the existing (should it exist) and get a valid file handle to it. If that fails, then the file is in use. The function does NOT actually create a file, nor does it alter the existing filessytem (I never do anything with the handle). It simply check whether I can get a file Handle to the file, should I want to do something with it.
This also works for files being opened from a share on a remote computer.
function IsFileInUse(fName: string): boolean;
var
HFileRes: HFILE;
begin
Result := false;
if not FileExists(fName) then
exit;
HFileRes := CreateFile(pchar(fName),
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;