Win32 API P-Invoke to bring a disk online, offline

2019-07-21 20:54发布

I am currently using Diskpart to accomplish these functions, but i would like to be able to use P-Invoke and not have to shell out to an external process in my C# app.

The example Diskpart scripts are:

//Online a disk
Select disk 7
disk online

// Reset GPT Identifier
select disk 7
UNIQUEID DISK ID=baf784e7-6bbd-4cfb-aaac-e86c96e166ee

I tried searching pinvoke.net but could only find functions that dealt with volumes, not disks. Any idea on how to accomplish these diskpart commands using Pinvoke?

2条回答
Explosion°爆炸
2楼-- · 2019-07-21 21:41

How about the DeviceIOControl API?

If not, I would look into using WMI. In particular, take a look at the WMI WIN32_DiskDrive or WIN32_LogicalDisk classes.

查看更多
男人必须洒脱
3楼-- · 2019-07-21 21:48

I'm not sure exactly what Dispart does but I'd suggest looking at WMI which might have this kind of functionality. It's usually a good place to look when you need to do something less common in Windows.

There's the Storage WMI Classes which might have something useful:
http://msdn.microsoft.com/en-us/library/ff567016%28VS.85%29.aspx

Edit: removed the suggestions about Volumes since I had missed that you're not interested in that.

查看更多
登录 后发表回答