How to Get Details of hardrive in C#

All tutorials created in C# to be posted in here.
6 posts Page 1 of 1
Contributors
User avatar
benji_19994
VIP - Donator
VIP - Donator
Posts: 156
Joined: Mon Apr 16, 2012 3:13 pm

Hello and welcome to my first C# tutorial

I'm going to be keeping all the controll names default names:

You Will need to be using :

using
Code: Select all
System.IO;
Alright so you will need to add a:

- listbox controll

To setup the listbox Controll you will need to insert

5 colums

-Drive
-Drive Name
-Drive Size
-Free Space
-Drive Type

You Will also need a contextmenu strip called
- Refresh


Now we are going To Create a Method

Fisrt Method:
getDriveInfo
Code: Select all
DriveInfo[] allDrives = DriveInfo.GetDrives();
void GetDriveInfo()
{

      foreach (DriveInfo d in allDrives)
            {
// The foreach statment meaning from mdsn
//The foreach statement repeats a group of embedded statements for each element in an array or an //object collection.
                try
                {
			//create a new list view item
			//this will also Hold the first coloms  
                    ListViewItem lvi = new ListViewItem(d.Name);
                    long ByteToGig = d.AvailableFreeSpace;
                    long TotalSpace = d.TotalSize;
                    lvi.SubItems.Add(ByteToGig / 1024 / 1024 / 1024 + " GB"); //adds free space to the listview in gb's
                    lvi.SubItems.Add(TotalSpace / 1024 / 1024 / 1024 + " GB");//adds total space to the listview in gb's
                    lvi.SubItems.Add(d.DriveFormat);//stores the Drive Format ect nfs ,fat 
                    lvi.SubItems.Add(d.VolumeLabel);//The name if the Drive;
                    listView1.Items.Add(lvi);//add all of the listviewitem into the listview

                }
                catch { }
            }
        }
Alright for the ContextMenuStrip
doubble click it for the click event and add
Code: Select all
            listView1.Items.Clear();//clears the ListViewe
            GetDriveInfo();//get's the drive info using the method we created earlyer

Now doubble click the form1 for the load event and add
Code: Select all
GetDriveInfo();
This is what the finished Product should look like
Image
and you should be done ='D
I will be coming back to this tutorial tomorrow and posting how to save the infomation to a Textfile on you desktop and stuff.
Last edited by benji_19994 on Mon Mar 11, 2013 10:37 pm, edited 1 time in total.
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

aa nice one brother keep it up :P
Find my programs on Softpedia
User avatar
Ffenixw0rks
VIP - Donator
VIP - Donator
Posts: 152
Joined: Sun Jun 19, 2011 2:51 pm

Not bad. But Free space is broken :)
Image
User avatar
benji_19994
VIP - Donator
VIP - Donator
Posts: 156
Joined: Mon Apr 16, 2012 3:13 pm

Ffenixw0rks wrote:
Not bad. But Free space is broken :)
I see i mixed them around ill change it when i get home ='D
User avatar
noypikami
VIP - Donator
VIP - Donator
Posts: 151
Joined: Sat Dec 22, 2012 1:49 am

nice tutorial( good work) , but the photo shows that the free space is greater than the drive size.
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

bro #benji_19994 i wrote this for you

Image :?

codes :
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//written by shim
//referrence http://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives.aspx
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form

    {
                
              public Form1()
        {
            InitializeComponent();
                  
        }

          
        private void Form1_Load(object sender, EventArgs e)
        {
                        foreach (System.IO.DriveInfo label in System.IO.DriveInfo.GetDrives())
            {

                if (label.IsReady)
                {
                    listBox1.Items.Add( " " + label.Name + " " + "TotalSize" + " " + label.TotalSize.ToString() + '\n');
                    listBox1.Items.Add( " " + label.Name + " " + "FreeSpace" + " " + label.TotalFreeSpace.ToString() + '\n');

                }
            }
        }


    }
}


:?
Find my programs on Softpedia
6 posts Page 1 of 1
Return to “C-Sharp Tutorials”