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"); |
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"); |
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"); |
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"); |
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"); |
The above code will create the new directory.
Delete:
The Delete method is used to delete the directory.
|
DirectoryInfo oDirInfo = new DirectoryInfo (@"c:\SampleDir"); |
GetFiles:
The GetFiles method is used to get the files in the specified folder.
|
DirectoryInfo oDirInfo = new DirectoryInfo(@"c:\Windows"); |
GetDirectories:
This method is used to get the sub directories in the given directory path.
|
DirectoryInfo oDirInfo = new DirectoryInfo(@"c:\windows"); |
MoveTo:
This method will be used to move an existing folder from one place to another place.
Screenshots of the Attached application:

Screenshot 1. Information about the given file

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

Screenshot 3. Show the file content of the given file

Screenshot 4. Information about the given directory

Screenshot 5. Show the files in the given directory.

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.

Kalaivani MahendranPosted Mar 25, 2011, 6:54 AM
hi, I am having number of files in directory. Intially i want to load the first file without passing query string, with the first file there should have next file link. Once the next link button is clicked then only have to pass the query string. how to pass the query string from second file to end of the file, DirectoryInfo oImageFolderInfo = new DirectoryInfo(this.ImagePath); FileInfo[] oFileList = oImageFolderInfo.GetFiles("*.*"); string fileName=string.Empty; if (Request.QueryString["filename"] != null) { fileName=(Request.QueryString["filename"]); } else { fileName = oFileList[0].Name; } HtmlImage imag = new HtmlImage(); imag.Src = Url + fileName; HtmlAnchor nextAnchor = new HtmlAnchor(); nextAnchor.Href=?? nextAnchor.InnerText = "Next>>"; HtmlAnchor prevAnchor = new HtmlAnchor(); prevAnchor.Href=?? How to proceed this same upto reaching the end of the file?
K boopathiPosted Mar 24, 2011, 7:22 AM
nice article anna by boopathi
Sachin GaikwadPosted Jun 22, 2010, 12:17 AM
Superb this article has cleared some of my File concepts. Thnks.
techmaniac jainPosted Oct 24, 2009, 8:33 AM
i want help that how can i search folder in d- drive without gettin an exception exception is occuring that "you cant access d:\system volume information"