Showing posts with label DirectoryInfo. Show all posts
Showing posts with label DirectoryInfo. Show all posts

Sunday, March 9, 2014

Get Directory Info in .Net

 DriveInfo , DirectoryInfo , FileInfo , StreamReader , StreamWriter

DirectoryInfo

  • The  System.IO  namespace  necessary  API  to  access  the  folders  (directories)  information also.
  • 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.
           Library: System.IO.DirectoryInfo
        This class object represents a folder on the file system. This able to get the information of the folder and also to perform certain operations on that folder.

Object Construction:
      Syn: DirectoryInfo obj = new DirectoryInfo(path of the directory);
     Ex:  DirectoryInfo obj = new DirectoryInfo(c:\\windows);
You can observe the list of all available properties, methods of this class.

Program of 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();
}
}
}

Download above code for Demo on DirectoryInfo : Download Links

Program for Creating Directory                                                       

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DirectoryInfoDemo2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the directory path:");
string directorypath = Console.ReadLine();
DirectoryInfo dinfo = new DirectoryInfo(directorypath);
if (!dinfo.Exists)
{
dinfo.Create();
Console.WriteLine("Directory created."); 
}
else
Console.WriteLine(directorypath + " is already exists.");
Console.Read();
}
}
}

Download above code for Demo on Create DirectoryInfo : Download Links


Program for Deleting Directory                                                        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DirectoryInfoDemo3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the directory path:");
string directorypath = Console.ReadLine();
DirectoryInfo dinfo = new DirectoryInfo(directorypath);
if (dinfo.Exists)
{
dinfo.Delete(true);
Console.WriteLine("Directory deleted.");
}
else
Console.WriteLine(directorypath + " doesn't exists.");
Console.Read();
}
}
}

Download above code for Demo on Deleting DirectoryInfo : Download Links 


 DriveInfo , DirectoryInfo , FileInfo , StreamReader , StreamWriter

System.IO : ( DriveInfo, DirectoryInfo.....)


DriveInfo

  • .NET support to access the available drives information on the computer such as C:\ drive, D:\ drive etc.
  • This drives may include with hard disks, CD-ROM drives, DVD-ROM drives, pen drives etc.
  • It offers to get the volume labels, total space and current free space of the drives etc.

           Library: System.IO.DriveInfo

The  DriveInfo class  object  can  represent  a  drive  on the  computer.  It offers  several  properties
and methods to access the related information of the drive.

Object Construction:

    Syn: DriveInfo obj = new DriveInfo(drive letter);
      Ex:  DriveInfo obj = new DriveInfo(c);
You can observe the list of all available properties, methods of this class.


Program For Single DriveInfo                                                          

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; 
namespace DriveInfoDemo
{
 class Program
 {
  static void Main(string[] args)
  {
   Console.WriteLine("Enter the drive letter (a to z):");
   string driveletter = Console.ReadLine();
   DriveInfo d = new DriveInfo(driveletter);
   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();
   }
  }
}

Download above code for Demo on Single DriveInfo : Download Links


Program for Multi DriveInfo                                                             

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; 
namespace GetDrivesDemo
{ 
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();
  }
 }
}

Download above code for Demo on Multiple DriveInfo : Download Links



 DriveInfo , DirectoryInfo , FileInfo, StreamReader , StreamWriter

Flag Counter