
Curious programmers don’t bound themselves in any limit up to which scope they will spread their programming knowledge. They always interested in learning new things. I am one of them. I also like to do some things those aren’t inside my scope f professional work. To retrieve drive info, access windows service/processes, detect removable disk/cd-dvd etc are such kind of interests for programmers as well as to me too ๐ . From such interest, I have done some code and here, from that experience, i will try to share a very simple way with c# code examples to get our computer’s logical drive’s basic information. This is helpful on projects which need to determine drive’s blank space/capacity/file system type, detect/access removable disks etc.
In theย first example, we will see how to get this information by utilizing MangementScope, ManagementObjectSearcher, ObjectQuery, ManagementObjectCollection, DirectoryInfo classes.
ManagementScope ms = new ManagementScope(); ObjectQuery oq = new ObjectQuery("SELECT DeviceID, VolumeName FROM Win32_LogicalDisk"); ManagementObjectSearcher mos = new ManagementObjectSearcher(ms, oq); ManagementObjectCollection moc = mos.Get(); foreach (ManagementObject mo in moc) { DirectoryInfo newDI = new DirectoryInfo(System.IO.Path.Combine(mo["DeviceID"].ToString(), " ")); newDI.Refresh(); FileInfo[] files = newDI.GetFiles(); FileSystemInfo[] info = newDI.GetFileSystemInfos(); //Now Access/Show/Return details information from "files" and "info" objects }
The above code snippet will require using System.Management and System.IO name space to be included to get access to the classes mentioned. Here you will notice an interesting object instance of ‘ObjectQuery’ class. Yes, as you were thinking, we can write SQL like query to retrieve information from .net objects. The current query will retrieve all logical drives. But, suppose, if you want information from only removable disks, then you can use a ‘WHERE’ just like on standard SQL query like this
SELECT DeviceID, VolumeName FROM Win32_LogicalDisk WHERE DriveType=2
Here ‘DriveType’ is an property of ‘Win32_LogicalDisk’ class and 2 values stands for removable disks. You can see more details about them on microsoft’s msdn online.
If you don’t have to retrieve specific type drive and want information from all drives, then you can also use ‘DriveInfo’ class. See the following code sample:
DriveInfo[] allDrives = DriveInfo.GetDrives(); foreach (DriveInfo d in allDrives) { Console.WriteLine("Drive {0}", d.Name); Console.WriteLine("File type: {0}", d.DriveType); }
The above code snippet will show drive name and drive’s file type on a console application. As long you are on system.io namespace, you won’t need to add any other thing to get this work.
If you have any specific question regarding retrieval of drive info in c#, please ask them here in comments. I will try to answer as soon as I can. Happy coding ๐
its nice and amazing .. i have a small doubt .. how to write queries in objectquery ?
if i want to get the list of all posssible resolutions of any display device then what is the query i have to write .. please suggest me … Thanks in advance
Why I Obtain the next message: {“The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)”}?
Not exactly sure, but are you running the application as administrator? This stackoverflow thread may help your further: http://stackoverflow.com/questions/4657724/wmi-the-rpc-server-is-unavailable-exception-from-hresult-0x800706ba-throws
How do I get only the floppy drives?
Thanks in advance.
Not sure, but should work in similar way. Are you getting any error? I don’t have a floppy drive anymore and can’t test, sorry.