How to Get Details of hardrive in C#
All tutorials created in C# to be posted in here.
6 posts
Page 1 of 1
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
- 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
doubble click it for the click event and add

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.
I'm going to be keeping all the controll names default names:
You Will need to be using :
using
Code: Select all
Alright so you will need to add a:System.IO;
- 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
Alright for the ContextMenuStripDriveInfo[] 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 { }
}
}
doubble click it for the click event and add
Code: Select all
Now doubble click the form1 for the load event and add
listView1.Items.Clear();//clears the ListViewe
GetDriveInfo();//get's the drive info using the method we created earlyer
Code: Select all
This is what the finished Product should look likeGetDriveInfo();

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.
Ffenixw0rks wrote:Not bad. But Free space is brokenI see i mixed them around ill change it when i get home ='D
nice tutorial( good work) , but the photo shows that the free space is greater than the drive size.
bro #benji_19994 i wrote this for you
:?
codes :

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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023