Monday, May 25, 2009

List all the Logical Drive in the Computer : Windows Application, C#

Using System.Management name space we can read the all operating system details and we query System_Win32 database in the following example I am trying to read all the logical drives in the computer and list them in a ListBox and select a drive and you can browse all the files in the drive.

So lets start creating the example, open visual studio and create a Windows Forms Application.

Add one list box and 2 buttons to the Form, you can below screen shot for the design

Design

Now we need to add the reference for the System.Management.dll to project so that we can make use of some class using which we can read all logical drives. Once you add the reference you need to import the System.Management namespace, you can refer below screen shot.

Reference

Now we are ready with Design part and reference part so let start putting code to retrieve drive, for that please double click on Get All Drives button and add following code, in the following code I am call a method GetAllDrives in which I am creating object for ManagementObjectSearcher and searching all the drives using “SELECT Caption, DeviceID FROM Win32_DiskDrive” query, please refer below screen shot for code

GetAllDrives

Now we are done with searching and listing all the drives in the local computer now we will add the code for opening the selected drive in listbox, please refer the below screen shot.

OpenDrive

We are now ready with our code just build, run and test.

output

Hope this helps.

Tags: Read all Logical Drives in Local Computer, C#, Windows Application, .NET

No comments:

Post a Comment