473,554 Members | 3,075 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14454
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*****@discus sions.microsoft .comwrote in message
news:31******** *************** ***********@mic rosoft.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 = GetLogicalDrive s();
for(int i = 0; i < 26; i++)
{
BOOL b = (driveMask & 1);
if( b )
{
drive[4] = 'A' + i;
//printf("Drive: %s\n", drive);
hDeviceHandle = CreateFile(driv e , 0, 0, NULL,
OPEN_EXISTING,F ILE_ATTRIBUTE_N ORMAL, NULL);
if (hDeviceHandle != (HANDLE)-1)
{
STORAGE_DEVICE_ NUMBER sdn;
DWORD returned;
if (DeviceIoContro l(
hDeviceHandle,I OCTL_STORAGE_GE T_DEVICE_NUMBER ,NULL,0,&sdn,si zeof(sdn),&retu rned,NULL));
{
printf("\tDevic e type: %d number: %d partition: %d\n",sdn.Devic eType,
sdn.DeviceNumbe r, sdn.PartitionNu mber);
}
}
}
}

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_diskparti tion ... 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.Collecti ons.Generic;
using System.Text;
using System.Manageme nt;
namespace ConsoleApplicat ion31 {
class Program {
static void Main(string[] args) {
ManagementScope scope = new ManagementScope (@"\root\cimv2" );
ObjectQuery query = new ObjectQuery("se lect * from
Win32_DiskParti tion");
ManagementObjec tSearcher searcher = new
ManagementObjec tSearcher(scope , query);
ManagementObjec tCollection drives = searcher.Get();
foreach (ManagementObje ct current in drives) {
Console.WriteLi ne("device id = " + current["deviceid"]);
ObjectQuery associators = new ObjectQuery("AS SOCIATORS OF
{Win32_DiskPart ition.DeviceID= \"" + current["deviceid"] + "\"} where
assocclass=Win3 2_LogicalDiskTo Partition");
searcher = new ManagementObjec tSearcher(scope , associators);
ManagementObjec tCollection disks = searcher.Get();
foreach (ManagementObje ct disk in disks) {
Console.WriteLi ne("\tdevice id = " + disk["deviceid"]);
}
}
}
}
}
Cheers,

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

"Zeeshan" <Ze*****@discus sions.microsoft .comwrote in message
news:BF******** *************** ***********@mic rosoft.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 = GetLogicalDrive s();
for(int i = 0; i < 26; i++)
{
BOOL b = (driveMask & 1);
if( b )
{
drive[4] = 'A' + i;
//printf("Drive: %s\n", drive);
hDeviceHandle = CreateFile(driv e , 0, 0, NULL,
OPEN_EXISTING,F ILE_ATTRIBUTE_N ORMAL, NULL);
if (hDeviceHandle != (HANDLE)-1)
{
STORAGE_DEVICE_ NUMBER sdn;
DWORD returned;
if (DeviceIoContro l(
hDeviceHandle,I OCTL_STORAGE_GE T_DEVICE_NUMBER ,NULL,0,&sdn,si zeof(sdn),&retu rned,NULL));
{
printf("\tDevic e type: %d number: %d partition: %d\n",sdn.Devic eType,
sdn.DeviceNumbe r, sdn.PartitionNu mber);
}
}
}
}

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******** ******@TK2MSFTN GP04.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_diskparti tion ... 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.Collecti ons.Generic;
using System.Text;
using System.Manageme nt;
namespace ConsoleApplicat ion31 {
class Program {
static void Main(string[] args) {
ManagementScope scope = new ManagementScope (@"\root\cimv2" );
ObjectQuery query = new ObjectQuery("se lect * from
Win32_DiskParti tion");
ManagementObjec tSearcher searcher = new
ManagementObjec tSearcher(scope , query);
ManagementObjec tCollection drives = searcher.Get();
foreach (ManagementObje ct current in drives) {
Console.WriteLi ne("device id = " + current["deviceid"]);
ObjectQuery associators = new ObjectQuery("AS SOCIATORS OF
{Win32_DiskPart ition.DeviceID= \"" + current["deviceid"] + "\"} where
assocclass=Win3 2_LogicalDiskTo Partition");
searcher = new ManagementObjec tSearcher(scope ,
associators);
ManagementObjec tCollection disks = searcher.Get();
foreach (ManagementObje ct disk in disks) {
Console.WriteLi ne("\tdevice id = " + disk["deviceid"]);
}
}
}
}
}
Cheers,

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

"Zeeshan" <Ze*****@discus sions.microsoft .comwrote in message
news:BF******** *************** ***********@mic rosoft.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 = GetLogicalDrive s();
for(int i = 0; i < 26; i++)
{
BOOL b = (driveMask & 1);
if( b )
{
drive[4] = 'A' + i;
//printf("Drive: %s\n", drive);
hDeviceHandl e = CreateFile(driv e , 0, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_ NORMAL, NULL);
if (hDeviceHandle != (HANDLE)-1)
{
STORAGE_DEVICE _NUMBER sdn;
DWORD returned;
if (DeviceIoContro l(
hDeviceHandle, IOCTL_STORAGE_G ET_DEVICE_NUMBE R,NULL,0,&sdn,s izeof(sdn),&ret urned,NULL));
{
printf("\tDevi ce type: %d number: %d partition: %d\n",sdn.Devic eType,
sdn.DeviceNumb er, sdn.PartitionNu mber);
}
}
}
}

Regards :)


Jul 7 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8457
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 it is a VERY difficult process for people used to apple products to handle. Now, to solve the problem, and possibly make it more mainstream, i...
6
8770
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 Drive information. I've figured out most of it, but here's the problem. The problem that I'm running into is that we have logical drives that have no...
0
2485
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: StringCollection propNames = new StringCollection(); ManagementClass driveClass = new ManagementClass("Win32_DiskDrive"); PropertyDataCollection props =...
7
24135
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: StringCollection propNames = new StringCollection(); ManagementClass driveClass = new ManagementClass("Win32_DiskDrive"); PropertyDataCollection props =...
3
2165
by: Arne Beruldsen | last post by:
How do you read the serial number to a hard drive? Thanks...Arne
14
28093
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) Win32_PhysicalMedia (a class) property SerialNumber." I'm sorry to admit it bit I am really undereducated on how to incorporate some of the Windows SDK...
2
7421
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 is in that case for normal users invisible in windows xp. i've searched the internet but i don't know where to look and i don;t know how to...
7
1734
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
1736
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, mapped into the server. I try to show an example
0
7512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7783
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6128
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
827
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.