Wednesday, December 7, 2011

Find the Drives and Directories in a Computer


using System.IO;
using System.Data;

public partial class Filepaths : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get the Drives or Disk info'n present in PC into DriveInfo[] Array..
DriveInfo[] AllDrives = DriveInfo.GetDrives();
foreach(DriveInfo drinfo in AllDrives)
{
Response.Write("Drive Name is "+drinfo.Name+"<br/>"+
"Drive type is "+drinfo.DriveType+"<br/>");
if (drinfo.IsReady == true)
{
Response.Write("VolumeLabel is "+drinfo.VolumeLabel+"<br />"+
"File System is "+drinfo.DriveFormat+"<br/>"+
"Avail Space is "+drinfo.AvailableFreeSpace+"<br />"+
"Avail Space is " + drinfo.TotalFreeSpace + "<br />" +
"Avail Space is " + drinfo.TotalSize + "<br />" +
"");
}
}


//Get the Directories info Present in a Drive "C:\"
DirectoryInfo di = new DirectoryInfo(@"C:\");
// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*w*");// + "*p*" + "*d*" + "*w*");
Response.Write(dirs.Length);
foreach (DirectoryInfo dinext in dirs)
{
Response.Write("the number of files in "+dinext+" is "+dinext.GetFiles().Length+"+<br/>");
}
}
}
==============================================
OutPut:

Drive Name is C:\
Drive file type Fixed
VolumeLabel is
File System is NTFS
Avail Space is 53018939392
Avail Space is 53018939392
Avail Space is 73402363904
Drive Name is D:\
Drive file type Fixed
VolumeLabel is
File System is NTFS
Avail Space is 167805022208
Avail Space is 167805022208
Avail Space is 176646078464
1the number of files in WINDOWS is 155+


Reference: