如何检查exe是否从服务器访问
这是一个客户端服务器应用程序。我正在创建一个更新程序,它将替换 exe 文件列表、运行脚本以及其他需要更新的内容。这将安装在服务器上。
首先,我需要检查可执行文件是否通过网络共享打开。我可以通过进入计算机管理然后共享文件和打开文件来手动执行此操作。这似乎是检查文件是否打开的唯一方法。我尝试使用 R/W 检查文件是否已打开,但这不起作用。查看了 Win32_ServerConnection,但这只是列出了打开的文件数量,而不是名称。
如果不能在 Delphi 中完成,我想在 Delphi 7 或 C# 中编写此代码。我发现了一些可以查看服务器上打开的文件的程序,但没有介绍如何做到这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你为什么不尝试删除它呢?如果文件当前已打开,您将收到错误消息。如果删除它有效,您只需复制更新的可执行文件即可。
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.
在 Windows 2000 及更高版本上,您可以使用 NetFileEnum API:
似乎在
LocalPath
参数中传递的路径必须以双反斜杠开头,例如:产生以下输出(如果文件通过共享打开):
另请注意,MSDN 说:
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:
我使用这样的函数来检查是否可以更改文件系统上的文件。基本上我尝试“创建”一个名为 fName 的新文件,仍然打开现有文件(如果存在)并获取它的有效文件句柄。如果失败,则该文件正在使用中。
该函数实际上并不创建文件,也不会更改现有的文件系统(我从未对句柄执行任何操作)。它只是检查我是否可以获得该文件的文件句柄,如果我想用它做一些事情。
这也适用于从远程计算机上的共享打开的文件。
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.
你见过System.IO中的FileSystemWatcher吗?
http://msdn.microsoft.com/en-us/库/system.io.filesystemwatcher.aspx
Have you seen the FileSystemWatcher in System.IO?
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx