
Curious programmers don’t bind themselves to any limit up to which scope they will spread their programming knowledge. They are 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 interest to programmers as well as to me :). Because of this interest, I have done some code, and from that experience, I will try to share a straightforward way with c# code examples to get basic information from our computer’s logical drive. This is helpful on projects which need to determine the 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, and 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
}
Code language: PHP (php)
The above code snippet will require using System. Management and System.IO namespace to be included to access the classes mentioned. Here, you will notice an exciting object instance of the ‘ObjectQuery’ class. Yes, as you were thinking, we can write SQL-like queries 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 a standard SQL query like this.
SELECT DeviceID, VolumeName FROM Win32_LogicalDisk WHERE DriveType=2
Here, ‘DriveType’ is a property of the ‘Win32_LogicalDisk’ class, and two values represent removable disks. You can find more details about them on Microsoft’s MSDN online.
If you don’t have to retrieve a specific type of drive and want information from all drives, then you can also use the ‘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);
}
Code language: PHP (php)
The above code snippet will show the drive name and the drive’s file type on a console application. As long as you are in the system.io namespace, you won’t need to add anything else to make this work.
If you have any specific questions regarding the retrieval of drive info in c#, please ask them here in the comments. I will try to answer as soon as I can. Happy coding 🙂
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
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.