By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,484 Members | 1,044 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,484 IT Pros & Developers. It's quick & easy.

How to get Hard Disk number from drive letter

P: n/a
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
Jul 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/20...t/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

"Zeeshan" <Ze*****@discussions.microsoft.comwrote in message
news:31**********************************@microsof t.com...
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

Jul 7 '06 #2

P: n/a
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 :)
Jul 7 '06 #3

P: n/a
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" <Ze*****@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
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 :)

Jul 7 '06 #4

P: n/a
sorry in typing it in I switched the order .. but same idea.
"Greg Young" <dr*******************@hotmail.comwrote in message
news:O0**************@TK2MSFTNGP04.phx.gbl...
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" <Ze*****@discussions.microsoft.comwrote in message
news:BF**********************************@microsof t.com...
>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,NUL L,0,&sdn,sizeof(sdn),&returned,NULL));
{
printf("\tDevice type: %d number: %d partition: %d\n",sdn.DeviceType,
sdn.DeviceNumber, sdn.PartitionNumber);
}
}
}
}

Regards :)


Jul 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.