473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to get Hard Disk number from drive letter

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
4 14429
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Erich Keane | last post by:
Ok, first a quick background: I, along with a small group of diehards came up with a way to disable the EU volume cap protection on the european ipods. Unfortunately, this way never caught on, as...
6
by: Steve Gerhart | last post by:
Hopefully someone can point me in the right direction. I'm try to construct a table that has the following information about disk drives on 2000 and NT 4.0 boxes. Size, Partitions and Logical...
0
by: john doe | last post by:
How can I use WMI or a WqlObjectQuery to find the hard drive letter of the physical drive location index. For example the following code will give me the physical drive location:...
7
by: jimdscudder | last post by:
How can I use WMI or a WqlObjectQuery to find the hard drive letter of the physical drive location index. For example the following code will give me the physical drive location:...
3
by: Arne Beruldsen | last post by:
How do you read the serial number to a hard drive? Thanks...Arne
14
by: Lauren Wilson | last post by:
Discovered this interesting comment on MSDN: "To programmatically obtain the hard disk's serial number that the manufacturer assigns, use the Windows Management Instrumentation (WMI)...
2
by: unknown; | last post by:
hi, i want to write an application who can acces a disk/partition in windows xp. I know how to do that if the disk/partition has a drive letter. But in this case the disk has no driveletter and...
7
by: Adele le Roux | last post by:
Hi All, How can I get the hard disk serial number of a remote computer's C:? The drive will NOT be mapped as a network drive. Thanks, Adele
3
by: massi183 | last post by:
Hi! I have a problem. I created a new "web site" with visual studio 2005 (asp net, default language vb). I need to move file from the server where there is my "web site" to another disk,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.