使磁盘联机/脱机
我有一个程序正在 Windows 中对磁盘进行原始 IO 操作。
如果目标磁盘在线,则一切正常。但是,某些 Windows 操作系统中的默认行为是让新磁盘最初处于脱机状态。
我很难找到正确的 API 在 Windows 上执行此操作。等效的命令行类似于:
"select disk 2", "online disk" | diskpart
但是我需要能够在代码中执行此操作。我查看了DeviceIoControl Win32 API(我认为是正确的),但无法确定要使用哪个控制代码。事实上,我找不到它,这让我觉得我可能缺少一个更好的 API 来使用。
I have a program that is doing raw IO to disks within Windows.
All works fine if the target disk is online. However, the default behavior in some Windows OSes is to have new disks initially offline.
I am having a hard time finding the correct API to do this on Windows. The command line equivalent would be something like:
"select disk 2", "online disk" | diskpart
However I need to be able to do this in code. I looked through the DeviceIoControl Win32 API (which I think is right) but cannot determine which control code to use. The fact that I can't find it makes me think I might be missing a better API to use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于后代来说,答案(在 Win 2k3/Vista 及更高版本上)是虚拟磁盘服务 (VDS)。需要做一些工作才能将它们整合在一起,特别是如果您在 .NET 中不经常使用 COM 对象的话。
磁盘联机/脱机通过 IVdsDrive::SetStatus 完成。至少应该;我发现只需禁用磁盘上的只读状态即可解决我的问题。我可以使用 IVdsDisk::SetFlags 来执行此操作 具有适当的标志值。
For future generations, the answer (on Win 2k3/Vista and later) is the Virtual Disk Service (VDS). There's some work getting it all together, especially if you don't use COM objects within .NET that much.
Disk online/offline is done with IVdsDrive::SetStatus. At least it should; I found that I could solve my problem with simply disabling read-only status on my disk. I was able to do this with IVdsDisk::SetFlags with the appropriate flag value.
不确定 C#,但我在 C++ 中使用它:
尝试使用
IOCTL_DISK_SET_DISK_ATTRIBUTES
调用DeviceIoControl()
。文件句柄必须具有读写访问权限。我认为它至少需要 Windows 7。它不适用于 Windows 2003 x64。 Windows 8 成功使磁盘脱机,然后您可以从备份重写它。Not sure about C#, but I'm using this in C++:
Try calling
DeviceIoControl()
withIOCTL_DISK_SET_DISK_ATTRIBUTES
. The file handle must have read and write access. I think it requires at least Windows 7. It doesn't work on Windows 2003 x64. Windows 8 successfully takes the disk offline and then you can rewrite it from a backup.这个问题 有几个指向 Windows API 的有用链接,包括 DeviceIOControl 方法。
查看所有枚举后,我找不到与使磁盘联机相关的任何内容,也找不到除格式化/分区之外对磁盘进行任何有趣的更改。这可能是因为此功能仅支持热插拔硬盘驱动器。热插拔硬盘驱动器的市场非常小,并且绝大多数情况下都有驱动程序来支持任何所需的操作。最后,其余的人应该能够使用 diskpart 工具来完成任何需要的事情。
我认为您需要再次考虑您的要求。您正在运行的进程具有使硬盘联机所需的权限,但无法访问命令行程序?以下是一些不使用命令行程序的常见原因的建议:
This question has a couple useful links to the Windows API, including the DeviceIOControl method.
After looking through all of the enumerations, I could not find anything related to bringing a disk online, or make any interesting change to the disk beyond formatting/partitions. This is likely because only hot-swappable hard drives are supported by this functionality. The market for hot-swappable hard drives is very small, and the vast majority of those situations there are drivers to support any needed operations. Finally the remainder should be able to use the diskpart tool for whatever is necessary.
You need to look again at your requirements I think. You are running a process that has the rights necessary to online a hard disk, but cannot access a command line program? Here are some suggestions for common reasons to not use a command line program:
使用DeviceIoControl和IOCTL_DISK_IS_WRITABLE控制代码,可以检查磁盘是否可写。如果磁盘离线则返回 false。这意味着可以确定磁盘是否脱机,并且在 Windows 2003 及更高版本中工作正常。但是,我找不到任何有用的 IOCTL 在 Windows 2003 上使磁盘联机。IOCTL_DISK_SET_DISK_ATTRIBUTES 仅适用于 Windows 2008 及更高版本。
Using DeviceIoControl and IOCTL_DISK_IS_WRITABLE control code, it is possible to check if disk is writable. If the disk is offline it returns false. This means that it is possible to determine if disk is offline and it works fine with Windows 2003 and after. However, I could not find any useful IOCTL to bring the disk online on Windows 2003. IOCTL_DISK_SET_DISK_ATTRIBUTES only works with Windows 2008 and after.