How to Retrieve All Local Drives in the DataGridView Using C#
Submitted by janobe on Monday, June 17, 2019 - 23:53.
This tutorial that I’m going to teach you is about how to retrieve all local drives in the datagridview using C#. This is a very useful method when you want to display all computer drives in the datagridview to see what drives are offered on your personal computer. Follow the step by step guide in creating this simple program.
The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
Step 2
Add a DataGridView inside the Form just like shown below.
Step 3
Press F7 to open the code editor. In the code editor, create a method for getting all local drives.- private void getAllDrives()
- {
- dataGridView1.Rows.Clear();
- dataGridView1.Columns.Add("Name", "Name");
- dataGridView1.Columns.Add("Letter", "Letter");
- dataGridView1.Columns.Add("Type", "Type");
- foreach (DriveInfo drive in DriveInfo.GetDrives())
- {
- string driveName, driveType,driveletter;
- driveName = drive.Name;
- driveType = "";
- driveletter = drive.Name;
- if(drive.IsReady && drive.VolumeLabel != "")
- {
- driveName = drive.VolumeLabel;
- }
- else
- {
- switch (drive.DriveType) {
- case DriveType.Fixed : driveName = "Local Disk"; break;
- case DriveType.CDRom : driveName = "CD-ROM"; break;
- case DriveType.Network : driveName = "Network Drive"; break;
- case DriveType.Removable : driveName = "Removable Disk"; break;
- }
- }
- switch (drive.DriveType)
- {
- case DriveType.Fixed: driveName = "Local Disk"; break;
- case DriveType.CDRom: driveName = "CD-ROM"; break;
- case DriveType.Network: driveName = "Network Drive"; break;
- case DriveType.Removable: driveName = "Removable Disk"; break;
- }
- dataGridView1.Rows.Add(driveName,driveletter,driveType);
- }
- }
Step 4
Execute the method that you have created in the first load of the form.- private void Form1_Load(object sender, EventArgs e)
- {
- getAllDrives();
- }
Add new comment
- 147 views