Tutorial: Working with Files and Directories

The FileSystemInfo Class

One of the rich experiences in working with .NET is a huge collection of Base Class Libraries. The .NET Framework class library is a library of classes, interfaces, and value types that are included in the Microsoft .NET Framework SDK. This library provides access to system functionality and is designed to be the foundation on which .NET Framework applications, components, and controls are built.

The System.IO namespace contains types that allow synchronous and asynchronous reading and writing on data streams and files.

To parse information of lots of files and directories, the FileSystemInfo class comes in handy. It is an abstract class and contains members (methods, properties...) that are common to file and directory manipulation. A FileSystemInfo object can represent either a file or a directory. The implementation of this abstract class is done in FileInfo and DirectoryInfo class. The Members would take full path (absolute or relative) or UNC path if they require path as one of the parameters.

In VB6 you could have used the Scripting Library (FileSystemObject) to manipulate the Files and directories.

Listing1: Members of FileSystemInfo class (for more information refer to MSDN)

Attributes Gets or sets the FileAttributes of the current FileSystemInfo.
CreationTime Gets or sets the creation time of the current FileSystemInfo object.
Exists Gets a value indicating whether the file or directory exists.
Extension Gets the string representing the extension part of the file.
FullName Gets the full path of the directory or file.
LastAccessTime Gets or sets the time the current file or directory was last accessed.
LastWriteTime Gets or sets the time when the current file or directory was last written to.
Name
For files, gets the name of the file. For directories, gets the name of the last directory in the hierarchy if a hierarchy exists. Otherwise, the Name property gets the name of the directory.
Delete Deletes a file or directory.
FullPath Represents the fully qualified path of the directory or file.
OriginalPath The path originally specified by the user, whether relative or absolute.

The FileInfo class and DirectoryInfo class extends FileSystemInfo class and each add some more members to themselves for specific operations.

The FileInfo Class

The FileInfo class provides instance methods for the creation, copying, deletion, moving, and opening of files, and provides members to create FileStream objects.

Listing2: List of some members of FileInfo class (for more information refer to MSDN). I have excluded the members of FileSystemInfo class overridden by FileInfo class and implemented by FileSystemInfo class.
 

Directory Gets an instance of the parent directory
DirectoryName Gets a string representing the directory's full path.
Length Gets the size of the current file or directory.
Name Gets the name of the file.
Create Creates a file.
CreateText Creates a StreamWriter that writes a new text file.
Open Opens a file with various read/write and sharing privileges
OpenRead Creates a read-only FileStream.
OpenText Creates a StreamReader with UTF8 encoding that reads from an existing text file.
OpenWrite Creates a write-only FileStream
AppendText
Creates a StreamWriter that appends text to the file represented by this instance of the FileInfo.
CopyTo Copies an existing file to a new file.
MoveTo moves  an existing file to a new location

The DirectoryInfo Class

The DirectoryInfo class provides instance methods for creating, moving, and enumerating through directories and subdirectories.

Listing3: List of some members of DirectoryInfo class (for more information refer to MSDN), I have excluded the members of FileSystemInfo Class overridden by Directory class and implemented by FileSystemInfo class.
 

Parent Gets the parent directory of a specified subdirectory
Root Gets the root portion of a path.
Create Creates a directory.
CreateSubDirectory Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.
GetDirectories Returns the subdirectories of the current directory
GetFiles Returns a file list from the current directory
GetFileSystemInfos Retrieves an array of strongly typed FileSystemInfo objects.
MoveTo Moves a DirectoryInfo instance and its contents to a new path.

Below is an example of extending the FileSystemInfo class and adding IsDirectory

using System;
using
System.IO;
public class
CFileSystem:FileSystemInfo
{
FileInfo mobjFileInfo;
DirectoryInfo mobjDirectoryInfo;
bool mblnIsDirectory = true
;
public CFileSystem(string
xstrFilePath)
{
try
{
mobjDirectoryInfo =
new
DirectoryInfo(xstrFilePath);
if
(!(mobjDirectoryInfo.Extension.Length==0))
{
mblnIsDirectory =
false
;
mobjDirectoryInfo=
null;
mobjFileInfo = new FileInfo(xstrFilePath);
}
}
catch
(Exception ex)
{
Console.WriteLine( ex.ToString());
}
public override void Delete()
{
if
(mblnIsDirectory)
{
mobjDirectoryInfo.Delete();
}
else
{
mobjFileInfo.Delete();
}
}
public override bool
Exists
{
get
{
if
(mblnIsDirectory)
{
return
mobjDirectoryInfo.Exists;
}
else
{
eturn
mobjFileInfo.Exists;
}
}
}
public string
Fullname
{
get
{
if
(mblnIsDirectory)
{
return
mobjDirectoryInfo.FullName;
}
else
{
return
mobjFileInfo.FullName;
}
}
}
public override string
Name
{
get
{
if
(mblnIsDirectory)
{
return
mobjDirectoryInfo.Name;
}
else
{
return
mobjFileInfo.Name;
}
}
}
public bool
IsDirectory
{
get
{
return
mblnIsDirectory;
}
}
public bool
IsFile
{
get
{
return
!mblnIsDirectory;
}
}
}


Similar Articles