Get DriveInfo in C#.NET


If we need to install any software (for example : visual studio 2010) requirement for the installation is 20gb space is required for the installation in C:\ Drive. we can check the available space in Drive's like C:/ D:/ ..E:/...

DriveInfo class :
---------------

.NET Support to access the avialable drives inforamtion on the computers such as C:\ or D:\ or E:\.....etc, this drives may include with System Hard Disk drive, CD-ROM drives, DVD-ROM Drives and Removable Drives (i.e Pen Drive) etc...
And it also offers to get name of the drive like volume labels and total amount of space in hard disk and current available free space of the drives etc...

Library is System.IO;

Syntax: Driveinfo diobj=new Driveinfo("drive name");
example : Driveinfo diobj=new Driveinfo("C:/");


we can access related inforamtion of the drive by using properties and methods.

Property and Method of Driveinfo :-
--------------------------------
1.) IsReady ----- Indicates that drive is ready or not (true / false)
2.) Name ----- Drive letter. (Ex: C:\ or D:\)
3.) DriveType ----- Type of the drive (like Fixed, CD Rom, Removable(i.e Pendrive))
4.) Volume Label ----- Name of Drive
5.) DriveFomrat ----- Drive Format like Fat, NTFS Fat32 Files..etc
6.) TotalSize ----- Total Memory of the Drive in byte/GB
7.) TotalFreeSpace --- Available Free Space in Drive show in byte/GB
8.) GetDrive() --- Display all available drives in the system

Drive Image

Property of Drive

Program for Single Drive Display :-
================================

 
using System.IO;

namespace GetSingleDriveInfo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the drive letter (a to z):");

// driveletter is nothing but a C:/ or D:/
string driveletter = Console.ReadLine();

// DriveInfo is a Predefined class of System.IO
DriveInfo d = new DriveInfo(driveletter);

// check for driveletter is ready or not if its ready go for process...display error
if (d.IsReady)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.DriveFormat);
Console.WriteLine(d.TotalSize + " bytes.");
Console.WriteLine(d.TotalFreeSpace + " bytes.");
}
else
Console.WriteLine(d.Name + " - " + " Not Ready.");
Console.Read();
}
}

Program for multiple Drive :-
==========================

uisng System.IO;

namespace getMultiDriveInfo
{
class Program
{
static void Main(string[] args)
{
DriveInfo[] dinfo = DriveInfo.GetDrives();
Console.WriteLine(dinfo.Length + " drives found on this computer.");
foreach (DriveInfo d in dinfo)
{
Console.WriteLine();
if (d.IsReady)
{
Console.WriteLine(d.Name);
Console.WriteLine(d.DriveType);
Console.WriteLine(d.VolumeLabel);
Console.WriteLine(d.DriveFormat);
Console.WriteLine(d.TotalSize + " bytes.");
Console.WriteLine(d.TotalFreeSpace + " bytes.");
}
else
Console.WriteLine(d.Name + " - " + d.DriveType + " - Not Ready.");
}
Console.Read();
}
}
}


Directory Info :
--------------

The System.IO namespace access the folders(directories) info,According to this, a folder is to be represented as an object.The folder object is able to get the details of the folder like folder full path, sub directories and files....etc.

Syn: DirectoryInfo obj = new DirectoryInfo( "path of the directory");
Ex: DirectoryInfo obj = new DirectoryInfo("c:\\windows");


Properties of DirectoryInfo class :-
---------------------------------

1.) Exists ---- Checks the existence of the directory. If it exists, it indicates
true. Otherwise, it indicates false.
2.) Name ---- Gets only the name of the directory
3.) FullName ---- Gets the name along with full path.
4.) CreationTime ---- Gets the date & time, when the directory is created.
5.) LastAccessTime --- Gets the date & time, when the directory is accessed last time.
6.) LastWriteTime --- Gets the date & time, when the directory is modified last time.
7.) Parent ---- Gets the directory object, which represents the respective parent folder.
8.) Root ---- Gets the name of the drive, in which the directory exists.

Methods of DirectoryInfo class :-
------------------------------

1.) Create() ---- Creates the a new directory.
2.) CreateSubdirectory(name) ---- Creates a sub directory.
3.) Delete() ---- Deletes the directory, if it is empty.
4.) Delete(true) ---- Deletes the entire directory, along with its sub directories and files.
5.) GetFiles() ---- Gets all the files in the directory as FileInfo class object array.
6.) GetFiles("search pattern") --- Gets the files in the directory as FileInfo class object array, based on the given search pattern.
7.) GetDirectories() ---- Gets all the directories in the directory as DirectoryInfo class object array.
8.) GetDirectories("search pattern") ---- Gets the directories in the directory as DirectoryInfo class object array, based on the given search pattern


Difference Between DriveInfo and DirectoryInfo :-
----------------------------------------------

Difference between Driveinfo and Directoryinfo


Program for DirectoryInfo :-
=========================

using System.IO;
namespace DirectoryInfoDemo1
{
class Program
{
static void Main(string[] args)
{
string directorypath = "c:\\windows\\help";
DirectoryInfo dinfo = new DirectoryInfo(directorypath);
if (dinfo.Exists)
{
Console.WriteLine("Directory Name: " + dinfo.Name);
Console.WriteLine("Directory Full Path: " + dinfo.FullName);
Console.WriteLine("\nCreated on: " + dinfo.CreationTime);
Console.WriteLine("Last accessed on: " + dinfo.LastAccessTime);
Console.WriteLine("Last modified on: " + dinfo.LastWriteTime);
Console.WriteLine("\nParent: " + dinfo.Parent.FullName);
Console.WriteLine("Root: " + dinfo.Root);
Console.WriteLine("\nFiles:");
FileInfo[] fobjs = dinfo.GetFiles();
foreach (FileInfo f in fobjs)
Console.WriteLine(f.FullName);
Console.WriteLine("\nSub Directories:");
DirectoryInfo[] dobjs = dinfo.GetDirectories();
foreach (DirectoryInfo d in dobjs)
Console.WriteLine(d.FullName);
}
else
Console.WriteLine(directorypath + " is not available on the system.");
Console.Read();
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: