我可以通过编程方式查明驱动器是否正在使用吗?

发布于 2024-09-04 15:00:04 字数 158 浏览 2 评论 0原文

有没有什么方法可以以编程方式查明映射驱动器当前是否正在使用?如果我尝试使用 net use 卸载驱动器,并且该驱动器正在使用中,它会告诉我该驱动器正在使用中并提示我采取进一步操作。

如果不再使用驱动器,我希望能够自动取消映射驱动器。系统是Windows XP。

Is there any way of programmatically finding out if a mapped drive is currently in use? If I try to unmount the drive with net use, and the drive is being used, it will tell me that the drive is in use and prompt me to take further action.

I'd like to be able to unmap drive automatically if it's no longer being used. The system is a Windows XP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-09-11 15:00:04

如果这是一个内部应用程序,作为一个低技术含量的简单解决方案,我建议从 Technet 并从您的进程执行,然后您的进程可以读取结果并过滤每个驱动器,以查看其中是否有任何一个位于您感兴趣的驱动器上。

请注意,我还没有检查 Handle 的许可条款实际上允许这种用法,但它们会在您第一次执行时弹出,以便您可以检查它们。

If this is an inhouse app, as a lowtech easy solution I'd suggest downloading the Handle app from Technet and executing from your process and then your process can read the results and filter per drive to see if any of them are on the drive you're interested in.

Please note, I've not checked that the license terms for Handle actually allows this usage but they'll pop up the first time you execute it so that you can check them then.

幻想少年梦 2024-09-11 15:00:04

在较高级别上,您需要枚举所有打开的文件句柄(或者更确切地说,所有打开的句柄,然后过滤除文件句柄之外的所有内容),然后查找其对象名称与映射网络驱动器的设备名称匹配的任何文件句柄。

它有很多低级 Win32 API(NtQuerySystemInformation、NtQueryInformationFile、NtQueryObject 等),但这绝对是可行的。如果您要走这条路,我建议您获取 Windows 的副本内部信息以及搜索 MSDN 和 Mark Russinovich 的博客

我认为您不会在这里得到完整的答案,除非可能已经有一个开源项目可以链接到它。例如,Sysinternals 工具的源代码曾经是免费提供的,并且仍然可以找到,如下所示 v4.3x 版本的 FileMon 源,这可能会带来一些启发。

At a high level, you would need to enumerate all open file handles (or rather all open handles, then filter out everything except the file handles) and then look for any file handles whose object name matches the mapped network drive's device name.

It's a lot of low-level Win32 APIs (NtQuerySystemInformation, NtQueryInformationFile, NtQueryObject, etc) but it's definitely doable. If you're going down this road, I would recommend picking up a copy of Windows Internals plus scouring MSDN and Mark Russinovich's blog.

I don't think it's the type of thing you'll get a complete answer to on here, unless perhaps there's an open source project already doing it which can be linked to. For instance, the source for the Sysinternals tools used to be freely available, and can still be found, like this v4.3x version of the FileMon source, which may shed some light.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文