How to get Hard Disk number from drive letter

G

Guest

hi,
i want to get the hard drive number for example if if have drive letter C it
should tell me Disk number as 1 and suppose if i have another hard disk
attach to my system having letter J, the by giving J it should give me disk
number 2. Any kind of help will be fuly appreciated

Regards,
Zeeshan
 
G

Greg Young

This article shows how to get the information I *think* you want through WMI
(i.e. it will tell you C is disk 0 partition 0, scroll about 1/2 way down
for an in depth explanation)
http://www.c-sharpcorner.com/Code/2004/Sept/WMIPart2.asp. Keep in mind that
C: is not a drive, it is a partition C and J could live on the same drive or
could live on different drives ...

I am assuming these numbers are what you want, if not can you be a bit more
clear in what number you are looking for?

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
 
G

Guest

Thanx for answering. I think you didnt get my question but as i solved my
problem , it like that

HANDLE hDeviceHandle = NULL;
char drive[] = {'\\', '\\', '.', '\\', 'A', ':', 0};
DWORD driveMask = GetLogicalDrives();
for(int i = 0; i < 26; i++)
{
BOOL b = (driveMask & 1);
if( b )
{
drive[4] = 'A' + i;
//printf("Drive: %s\n", drive);
hDeviceHandle = CreateFile(drive , 0, 0, NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
if (hDeviceHandle != (HANDLE)-1)
{
STORAGE_DEVICE_NUMBER sdn;
DWORD returned;
if (DeviceIoControl(
hDeviceHandle,IOCTL_STORAGE_GET_DEVICE_NUMBER,NULL,0,&sdn,sizeof(sdn),&returned,NULL));
{
printf("\tDevice type: %d number: %d partition: %d\n",sdn.DeviceType,
sdn.DeviceNumber, sdn.PartitionNumber);
}
}
}
}

Regards :)
 
G

Greg Young

This is the exact same information WMI gives you that I posted (did you read
through until about 1/2 way down where it explains how disk information
works?).. Device type is on win32_diskpartition ... it explains the mapping
with the logicaldisk record ..here is some simple code it was leading
towards as an example (it explained the associators etc).

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace ConsoleApplication31 {
class Program {
static void Main(string[] args) {
ManagementScope scope = new ManagementScope(@"\root\cimv2");
ObjectQuery query = new ObjectQuery("select * from
Win32_DiskPartition");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection drives = searcher.Get();
foreach (ManagementObject current in drives) {
Console.WriteLine("device id = " + current["deviceid"]);
ObjectQuery associators = new ObjectQuery("ASSOCIATORS OF
{Win32_DiskPartition.DeviceID=\"" + current["deviceid"] + "\"} where
assocclass=Win32_LogicalDiskToPartition");
searcher = new ManagementObjectSearcher(scope, associators);
ManagementObjectCollection disks = searcher.Get();
foreach (ManagementObject disk in disks) {
Console.WriteLine("\tdevice id = " + disk["deviceid"]);
}
}
}
}
}


Cheers,

Greg Young
MVP C#
http://codebetter.com/blogs/gregyoung
 
G

Greg Young

sorry in typing it in I switched the order .. but same idea.
Greg Young said:
This is the exact same information WMI gives you that I posted (did you
read through until about 1/2 way down where it explains how disk
information works?).. Device type is on win32_diskpartition ... it
explains the mapping with the logicaldisk record ..here is some simple
code it was leading towards as an example (it explained the associators
etc).

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
namespace ConsoleApplication31 {
class Program {
static void Main(string[] args) {
ManagementScope scope = new ManagementScope(@"\root\cimv2");
ObjectQuery query = new ObjectQuery("select * from
Win32_DiskPartition");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection drives = searcher.Get();
foreach (ManagementObject current in drives) {
Console.WriteLine("device id = " + current["deviceid"]);
ObjectQuery associators = new ObjectQuery("ASSOCIATORS OF
{Win32_DiskPartition.DeviceID=\"" + current["deviceid"] + "\"} where
assocclass=Win32_LogicalDiskToPartition");
searcher = new ManagementObjectSearcher(scope,
associators);
ManagementObjectCollection disks = searcher.Get();
foreach (ManagementObject disk in disks) {
Console.WriteLine("\tdevice id = " + disk["deviceid"]);
}
}
}
}
}


Cheers,

Greg Young
MVP C#
http://codebetter.com/blogs/gregyoung

Zeeshan said:
Thanx for answering. I think you didnt get my question but as i solved my
problem , it like that

HANDLE hDeviceHandle = NULL;
char drive[] = {'\\', '\\', '.', '\\', 'A', ':', 0};
DWORD driveMask = GetLogicalDrives();
for(int i = 0; i < 26; i++)
{
BOOL b = (driveMask & 1);
if( b )
{
drive[4] = 'A' + i;
//printf("Drive: %s\n", drive);
hDeviceHandle = CreateFile(drive , 0, 0, NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
if (hDeviceHandle != (HANDLE)-1)
{
STORAGE_DEVICE_NUMBER sdn;
DWORD returned;
if (DeviceIoControl(
hDeviceHandle,IOCTL_STORAGE_GET_DEVICE_NUMBER,NULL,0,&sdn,sizeof(sdn),&returned,NULL));
{
printf("\tDevice type: %d number: %d partition: %d\n",sdn.DeviceType,
sdn.DeviceNumber, sdn.PartitionNumber);
}
}
}
}

Regards :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top