Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

c# - How to identify if a drive is virtual or physical

This came up from my other question about IMAPI2 is it possible to identify if a DVD/CD drive is virtual and not physical?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The purpose of a virtual drive is to act exactly as its physical counterpart, just without the physical media. Both kinds of drives work with the operating system in the same manner as a device driver. I highly doubt the difference between them would be visible within the Win32 API. That would be counter-intuitive to the virtual drive's purpose.

I looked at the information provided by WMI (which is accessible in C#) and found something of interest. The device id of my virtual drive began with ‘SCSI’ whereas the device id of my physical SATA drive began with ‘IDE’. I believe most of (all of?) virtual drive software emulates a SCSI drive; I'm not sure. Typically, a user would have either an IDE or SATA optical drive which would both have an id beginning with ‘IDE’.

Virtual Drive Device: "SCSICDROM&VEN_ELBY&PROD_CLONEDRIVE&REV_1.41&00000000&0&000000"
Real Drive Device: "IDECDROMASUS_DRW-24B1ST_________________________1.03____5&295AF142&0&5.0.0"

Notice in my example device ids that the virtual drive is clearly identified as the Clone Drive software. You could check the manufacturer and product name against a known list of virtual drive software. This might yield a lot of false negatives and be very hard to maintain.

Either way, I am not confident that searching for features in the device id would be a highly reliable solution. There might be virtual drives which identify themselves differently. I only tested Clone Drive and Daemon Tools in researching your question.

If you were to use this approach for copyright protection (what else would you use it for?) then you have to consider if the chance of a false virtual drive determination is worth angering your customers.

Here is the C# code for inspecting the drives using WMI and accessing the device id. You will need to reference the System.Management assembly.

string driveLetter = "F";
ManagementObjectSearcher diskQuery = new ManagementObjectSearcher(String.Format("SELECT * FROM Win32_CDROMDrive WHERE Drive='{0}:'", driveLetter));
ManagementObject diskResult = diskQuery.Get().OfType<ManagementObject>().SingleOrDefault();
string deviceID = null;
if (diskResult != null)
    deviceID = (string)diskResult["DeviceID"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...