473,386 Members | 1,720 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,386 software developers and data experts.

How to detect hard drives

I need to detect the root directories of the installed hard disks (es: C:,
D:, E:, etc.). I'm not interested in cd drives.
I know that there is a way (a bit slow, if I remeber) to retrive all the
drives using a code like this:

ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");

ManagementObjectCollection disks = diskClass.GetInstances();

foreach (ManagementObject disk in disks) {

Console.WriteLine("Disk = " + disk["deviceid"]);

}

But I need only hard drives.
Nov 16 '05 #1
3 11638
"Flix" <wr***@newsgroup.comeon> wrote in
news:cm**********@lacerta.tiscalinet.it:
I need to detect the root directories of the installed hard
disks (es: C:, D:, E:, etc.). I'm not interested in cd drives.
I know that there is a way (a bit slow, if I remeber) to retrive
all the drives using a code like this:

ManagementClass diskClass = new
ManagementClass("Win32_LogicalDisk");

ManagementObjectCollection disks = diskClass.GetInstances();

foreach (ManagementObject disk in disks) {

Console.WriteLine("Disk = " + disk["deviceid"]);

}

But I need only hard drives.


Flix,

You can use the System.Environment.GetLogicalDrives method to get all
of the drives on your system, and then use an API call to
GetDriveType to filter out the hard drives.

// From Winbase.h
public enum DriveType : int
{
Unknown = 0,
NoRoot = 1,
Removable = 2,
Localdisk = 3,
Network = 4,
CD = 5,
RAMDrive = 6
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GetDriveType(string lpRootPathName);

// Example usage:
using System;
....
foreach (string s in Environment.GetLogicalDrives())
Console.WriteLine(string.Format("Drive {0} is a {1}.",
s, Enum.GetName(typeof(DriveType), GetDriveType(s))));
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #2
Flix wrote:
I need to detect the root directories of the installed hard disks (es: C:,
D:, E:, etc.). I'm not interested in cd drives.


using System;
using System.Management;

string query = "select name from win32_logicaldisk where drivetype=3";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementObject manobj in searcher.Get()) {
// Do Something
}

--
Take care,
Ken
(to reply directly, remove the cool car. <sigh>)
Nov 16 '05 #3
Thank you.
Nov 16 '05 #4

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

Similar topics

3
by: RunLevelZero | last post by:
I need a way to detect hard drives and their partitions... labels would be nice too... I did some googling but did not find anything all too useful. This will strictly be on Linux / Unix so any...
0
by: Captain | last post by:
Wanted older dead hard drives, any size. my son is looking for the magnets from these drives for a school wind generator project, if anyone has any dead drives laying around and willing to donate...
3
by: Ned Hart | last post by:
I'm formatting my mirrored drives and installing a third drive for raid 5 to add more space. I have backup exec. Can anyone tell me the best way to backup SQL so I can be sure it will restore...
36
by: Ron Johnson | last post by:
http://hardware.devchannel.org/hardwarechannel/03/10/20/1953249.shtml?tid=20&tid=38&tid=49 -- ----------------------------------------------------------------- Ron Johnson, Jr....
2
by: usmcsamhouston | last post by:
the other day i wiped a virus off my computer. Ever since then I haven't been able to just double-click my hard drives in My Computer. When i do double-click it, It opens the "open with" window. So...
0
by: Tim Golden | last post by:
Ohad Frand wrote: You can do this with WMI under Windows: <code> import wmi c = wmi.WMI () for i in c.Win32_LogicalDisk (): print i.Caption
0
by: =?Utf-8?B?S2VsbHk=?= | last post by:
Each fall and winter I deal witht he same problem. When the daylight savings time comes into effect the time stamp for files on my external portable hard drive are 1 hour out from the time stamps...
1
by: scotter86 | last post by:
Hey everyone, for the love of god i cannot get this straight. I have 2 hard drives, each runing windows xp home. I installed linux on my second hard drive and it over wrote the windows mbr. thats...
2
dlite922
by: dlite922 | last post by:
// go to "My Question" below to skip back story and rant. Windows 7 is still Microsoft ****. Problem: Windows 7 no longer recognizes some partitions on different disk that were untouched...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.