What are
File System Classes?
System.IO
namespace contains classes to obtain information and manipulate file,
directories and drives. Most of the classes inherit from FileSystemInfo, and they are named like FileInfo, DirectoryInfo.
DriveInfo class
provides information about a drive but it doesn’t inherit from FileSystemInfo.
Utility classes expose static methods to perform
certain operations on FileSystem. These classes include File, Directory, and Path.
FileSystemInfo
Class:
Public Methods:
Name
|
Description
|
Delete
|
Deletes a file or directory.
|
Refresh
|
Refreshes the state of the object.
|
Public Properties:
Name
|
Description
|
Attributes
|
Gets or sets the FileAttributes of the current FileSystemInfo.
|
CreationTime
|
Gets or sets the creation
time of the current FileSystemInfo object.
|
CreationTimeUtc
|
Gets or sets the creation
time, in coordinated universal time (UTC), 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.
|
LastAccessTimeUtc
|
Gets or sets the time, in
coordinated universal time (UTC), that the current file or directory was last
accessed.
|
LastWriteTime
|
Gets or sets the time when
the current file or directory was last written to.
|
LastWriteTimeUtc
|
Gets or sets the time, in
coordinated universal time (UTC), 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.
|
The FileInfo class:
The
FileInfo class provides basic functionality to access and manipulate a single
file in the file system.
FileInfo Properties:
Name
|
Description
|
Directory
|
Gets an instance of the parent directory.
|
DirectoryName
|
Gets a string representing the directory's
full path.
|
IsReadOnly
|
Gets or sets a value that determines if the
current file is read only.
|
Length
|
Gets the size, in bytes, of the current
file.
|
FileInfo Methods:
Name
|
Description
|
AppendText
|
Creates a StreamWriter that appends text to
the file represented by this instance of the FileInfo.
|
CopyTo
|
Overloaded. Copies an existing file to a new
file.
|
Create
|
Creates a file.
|
CreateText
|
Creates a StreamWriter that writes a new
text file.
|
Decrypt
|
Decrypts a file that was encrypted by the
current account using the Encrypt() method.
|
Delete
|
Permanently deletes a file. (Overrides .Delete().)
|
Encrypt
|
Encrypts a file so that only the account
used to encrypt the file can decrypt it.
|
GetAccessControl
|
Overloaded. Gets a FileSecurity object that
encapsulates the access control list (ACL) entries for the file described by
the current FileInfo object.
|
GetObjectData
|
Sets the SerializationInfo object with the
file name and additional exception information. (Inherited from FileSystemInfo.)
|
MoveTo
|
Moves a specified file to a new location,
providing the option to specify a new file name.
|
Open
|
Overloaded. 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.
|
Refresh
|
Refreshes the state of the object.
(Inherited from FileSystemInfo.)
|
Replace
|
Overloaded. Replaces the contents of a
specified file with the file described by the current FileInfo object,
deleting the original file, and creating a backup of the replaced file.
|
SetAccessControl
|
Applies access control list (ACL) entries
described by a FileSecurity object to the file described by the current FileInfo
object.
|
ToString
|
Returns the path as a string. (Overrides .ToString().)
|
Getting Information about a File:
FileInfo vBootIniInfo = new
FileInfo(“C:\\boot.ini”);
If(vBootIniInfo.Exists)
{
Console.WriteLine(vBootIniInfo.Name
+ “ : ”+ vBootIniInfo.FullName);
}
To Copy/Move a File use:
vBootIniInfo.CopyTo(“C:\\boot.bak”);
vBootIniInfo.MoveTo(“C:\\boot.bak”);
DirectoryInfo Class:
Provides
basic functionality to access and manipulate a single directory.
DirectoryInfo Properties:
Parent: Parent directory
Root: Root directory
DirectoryInfo Methods:
Name
|
Description
|
Create
|
Overloaded. Creates a directory.
|
CreateSubdirectory
|
Overloaded. Creates a subdirectory or
subdirectories on the specified path. The specified path can be relative to
this instance of the DirectoryInfo class.
|
Delete
|
Overloaded. Overridden. Deletes a
DirectoryInfo and its contents from a path.
|
Equals
|
Overloaded. Determines whether two Object
instances are equal. (Inherited from Object.)
|
GetDirectories
|
Overloaded. Returns the subdirectories of
the current directory.
|
GetFiles
|
Overloaded. Returns a file list from the
current directory.
|
GetFileSystemInfos
|
Overloaded. Retrieves an array of strongly
typed FileSystemInfo objects representing files and subdirectories of the
current directory.
|
MoveTo
|
Moves a DirectoryInfo instance and its
contents to a new path.
|
Refresh
|
Refreshes the state of the object.
(Inherited from FileSystemInfo.)
|
ToString
|
Overridden. Returns the original path that
was passed by the user.
|
Enumerating Files in a Directory:
DirectoryInfo dr = new DirectoryInfo
(“C:\\windows”);
foreach(FileInfo file in dr.GetFiles())
{
Console.WriteLine(“:\\”
+ file.Name);
}
DriveInfo class:
One can query available drives and capacity
related information.
DriveInfo Properties:
Name
|
Description
|
AvailableFreeSpace
|
Indicates the amount of
available free space on a drive.
|
DriveFormat
|
Gets the name of the file
system, such as NTFS or FAT32.
|
DriveType
|
Gets the drive type.
|
IsReady
|
Gets a value indicating
whether a drive is ready.
|
Name
|
Gets the name of a drive.
|
RootDirectory
|
Gets the root directory of a
drive.
|
TotalFreeSpace
|
Gets the total amount of free
space available on a drive.
|
TotalSize
|
Gets the total size of
storage space on a drive.
|
VolumeLabel
|
Gets or sets the volume label
of a drive.
|
DriveInfo Methods:
GetDrives: A static method that
returns all the available drives.
DriveType Enumeration:
Enum
|
Description
|
CDRom
|
The drive is an optical disc device, such as
a CD or DVD-ROM.
|
Fixed
|
The drive is a fixed disk.
|
Network
|
The drive is a network drive.
|
NoRootDirectory
|
The drive does not have a root directory.
|
Ram
|
The drive is a RAM disk.
|
Removable
|
The drive is a removable storage device,
such as a floppy disk drive or a USB flash drive.
|
Unknown
|
The type of drive is unknown.
|
Enumerating Drives:
DriveInfo[] dinfos =
DriveInfo.GetDrives();
foreach(DriveInfo vinfo in dinfos)
Console.WriteLine(vinfo.Name
+ “ : ” + vinfo.DriveType + “\n”);
Path Class:
Helps in
manipulating file system paths.
Name
|
Description
|
ChangeExtension
|
Changes the extension of a path string.
|
Combine
|
Combines two path strings.
|
GetDirectoryName
|
Returns the directory information for the
specified path string.
|
GetExtension
|
Returns the extension of the specified path
string.
|
GetFileName
|
Returns the file name and extension of the
specified path string.
|
GetFileNameWithoutExtension
|
Returns the file name of the specified path
string without the extension.
|
GetFullPath
|
Returns the absolute path for the specified
path string.
|
GetInvalidFileNameChars
|
Gets an array containing the characters that
are not allowed in file names.
|
GetInvalidPathChars
|
Gets an array containing the characters that
are not allowed in path names.
|
GetPathRoot
|
Gets the root directory information of the specified
path.
|
GetRandomFileName
|
Returns a random folder name or file name.
|
GetTempFileName
|
Creates a uniquely named, zero-byte
temporary file on disk and returns the full path of that file.
|
GetTempPath
|
Returns the path of the current system's
temporary folder.
|
HasExtension
|
Determines whether a path includes a file
name extension.
|
IsPathRooted
|
Gets a value indicating whether the
specified path string contains absolute or relative path information.
|
The FileSystemWatcher class:
WaitForChanged
|
Overloaded. A synchronous method that
returns a structure that contains specific information on the change that
occurred.
|
Properties:
Name
|
Description
|
Container
|
Gets the IContainer that contains the Component.
(Inherited from Component.)
|
EnableRaisingEvents
|
Gets or sets a value indicating whether the
component is enabled.
|
Filter
|
Gets or sets the filter string used to
determine what files are monitored in a directory.
|
IncludeSubdirectories
|
Gets or sets a value indicating whether
subdirectories within the specified path should be monitored.
|
InternalBufferSize
|
Gets or sets the size of the internal
buffer.
|
NotifyFilter
|
Gets or sets the type of changes to watch
for.
|
Path
|
Gets or sets the path of the directory to
watch.
|
Site
|
Gets or sets an ISite for the FileSystemWatcher. (Overrides .Site.)
|
SynchronizingObject
|
Gets or sets the object used to marshal the
event handler calls issued as a result of a directory change.
|
Events:
Name
|
Description
|
Changed
|
Occurs when a file or directory in the
specified Path is changed.
|
Created
|
Occurs when a file or directory in the
specified Path is created.
|
Deleted
|
Occurs when a file or directory in the
specified Path is deleted.
|
Renamed
|
Occurs when a file or directory in the
specified Path is renamed.
|
Comments