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.
On Windows 2000 and later, you could use NetFileEnum API:
It seems that the path passed in the
LocalPath
parameter has to begin with a double backslash, e.g.:produces the following output (if the file is open through a share):
Also note that MSDN says:
Have you seen the FileSystemWatcher in System.IO?
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
Why don't you just try to delete it? You will get an error if the file is currently open. And if deleting it works, you can just copy the updated executable.
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.
You could keep a static list of "servers" that this will run on, you can lookup the process machine name, hostname, or computer name for a match. If its in the list you can assume its open local.