Reader Level:
ARTICLE

Working with FileInfo and DirectoryInfo classes

Posted by Senthilkumar Articles | Files, Directory, IO August 11, 2009
This article explains about the FileInfo and DirectoryClass in the System.IO namespace. The sample application demonstrates how it works.
  • 0
  • 0
  • 26303
Download Files:
 

Introduction:

This article will explain about the Files and directory information and its operations. I have attached the sample web applications for FileInfo and DirectoryInfo classes. Let us see about these two classes.

Basics

The System.IO is one of the significant names which are used to work with the files in .Net framework. The FileInfo class which gives all the information about a file and DirectoryInfo class which gives all the information about a directory.

FileInfo class will be used to get the information about a file path, extension, create time, last access time, last write time and length,etc..,It has few methods like delete and exists.
 
FileInfo


The FileInfo class resides in the System.IO namespace. This is used to get the information about a file stored on the server.
 
The properties of the FileInfo class which inherits from the FileSystemInfo class.


S.No Property Description
1 CreationTime This property returns the creation of the file date and time.
2 CreationTimeUtc It returns the creation of the file in Universal time
3 Directory It returns the parent directory name
4 DirectoryInfo It returns the full directory path
5 Exists It returns whether the file exists or not as Boolean result.
6 Extension It returns the type of the file in the extension name
7 FullName It returns the full name of the file from the root directory.
8 IsReadOnly It returns the file is read only or not in the Boolean result.
9 Name It returns the name of the file
10 LastAccessTime It returns the date and time of the last access time
11 LastAccessTimeUtc It returns the last accessing date and time in universal format.
12 LastWriteTime It returns the last file saving date and time
13 LastWriteTimeUtc It returns the last write in the universal format
14 Length It returns the size of the file content in the bytes format.

The methods of the FileInfo class.
 
The FileInfo class helps to work with the files like reading the existing file content, creating a new file which as specified in the FileInfo path, add the content to the existing file, copy the file into another new file, delete the exiting created file, move the file one place into another place.
 
CreateText:


This method is used to create a new file. The content can be written into the newly created file.

FileInfo oFileInfo = new FileInfo(@"e:\SampleFile.txt");
StreamWriter
oStreamWriter = oFileInfo.CreateText();
oStreamWriter.WriteLine("-----------------------------------");
oStreamWriter.WriteLine("Sample file creation");
oStreamWriter.WriteLine("Senthilkumar");
oStreamWriter.WriteLine("-----------------------------------");

oStreamWriter.Close();


In the above code example, I have created a new file and added some content to that file.
 
OpenText:
 
The OpenText method is used to read the content from the existing file.


FileInfo oFileInfo = new FileInfo(@"e:\SampleFile.txt");
StreamReader
oStreamReader = oFileInfo.OpenText();
Console
.WriteLine(oStreamReader.ReadToEnd());
oStreamReader.Close();


In the above example, whatever the content available in the SampleFile.txt will be printed.
 
Create:
 
This method is used to create the new file.


FileInfo oFileInfo = new FileInfo(@"c:\NewFile.dat");
FileStream
oFileStream = oFileInfo.Create();
oFileStream.Close();


The above sample code will be created a new file name called NewFile in the c root directory.
 
Delete:

This method is used to delete the existing file.


FileInfo oFileInfo = new FileInfo(@"c:\NewFile.dat");
If (oFileInfo.Exists)
{
    oFileInfo.Delete ();

}


CopyTo and MoveTo

The CopyTo method is used to copy the existing file into new file.

The MoveTo method is used to move the file one place to another valid location.

DirectoryInfo

The DirectoryInfo class is used to get the information about a directory and some operations like delete, create new directory and sub directories.

There are methods to get the available files and directories.

Let us see the properties of the DirectoryInfo class

S.No Property Description
1 CreationTime This property returns the creation of the directory date and time.
2 CreationTimeUtc It returns the creation of the file in Universal time
3 Exists It returns whether the directory exists or not as Boolean result.
4 Extension It returns the type of the file in the extension name
5 FullName It returns the full name of the file from the root directory.
6 Name It returns the name of the directory
7 LastAccessTime It returns the date and time of the last access time
8 LastAccessTimeUtc It returns the last accessing date and time in universal format.
9 LastWriteTime It returns the last file saving date and time
10 LastWriteTimeUtc It returns the last write in the universal format
11 Root It returns the Root directory.
12 Parent It returns the Parent directory of the current directory
13 LastWriteTimeUtc It returns the last write in the universal format
14 Length It returns the size of the file content in the bytes format.

Let us see the some of the methods in the DirectoryInfo class.


Create:

This method is used to create a new directory.

DirectoryInfo oDirInfo = new DirectoryInfo (@"c:\SampleDir");
If(oDirInfo.Exists)
{
    Console.WriteLine("Directory already exists!");
    Return;
}
oDirInfo.Create();

Console
.WriteLine("Directory created!!");


The above code will create the new directory. 
 
Delete:


The Delete method is used to delete the directory.

DirectoryInfo oDirInfo = new DirectoryInfo (@"c:\SampleDir");
if
(oDirInfo.Exists)
{
    oDirInfo.Delete();
    Console.WriteLine("Directory Deleted!");
}
Else
{
    Console.WriteLine("Directory doesnot exists!");

}


GetFiles:
 
The GetFiles method is used to get the files in the specified folder.


DirectoryInfo oDirInfo = new DirectoryInfo(@"c:\Windows");
if
(oDirInfo.Exists)
{
    FileInfo[] Files = oDirInfo.GetFiles();
    foreach (object FileName in Files)
    {
        Console.WriteLine(FileName.ToString());
    }

}


GetDirectories:
 
This method is used to get the sub directories in the given directory path.


DirectoryInfo oDirInfo = new DirectoryInfo(@"c:\windows");
if
(oDirInfo.Exists)
{
    DirectoryInfo[] Dirs = oDirInfo.GetDirectories();
    foreach (object DirNames in Dirs)
    {
        Console.WriteLine(DirNames.ToString());
    }
}
Else
{
   Console.WriteLine("The given directory doesnot exists");

}


MoveTo:

This method will be used to move an existing folder from one place to another place.
 
Screenshots of the Attached application:


 
FileDirInfo1.JPG
Screenshot 1.  Information about the given file

 
FileDirInfo2.JPG
Screenshot 2.  Delete the given file after getting confirmation from the user

 
FileDirInfo3.JPG
Screenshot 3.  Show the file content of the given file

 FileDirInfo4.JPG

Screenshot 4.  Information about the given directory

 FileDirInfo5.JPG

Screenshot 5. Show the files in the given directory.

 FileDirInfo6.JPG

Screenshot 6.  Show the sub directories in the given directory path.

Conclusion:

This two classes are very useful when we work with the files and directories. I have attached the sample we applications for this two classes. If there is any mistake then please notify me. I expect your valuable comments and feedback about this article.

COMMENT USING

Trending up