CHAPTER 20 FILE I/O AND OBJECT SERIALIZATION
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
Obtains the name of the current file or directory.
FileSystemInfo also defines the Delete() method. This is implemented by derived types to delete a
given file or directory from the hard drive. Also, you can call Refresh() prior to obtaining attribute
information to ensure that the statistics regarding the current file (or directory) are not outdated.
Working with the DirectoryInfo Type
The first creatable I/O-centric type you will examine is the DirectoryInfo class. This class contains a set
of members used for creating, moving, deleting, and enumerating over directories and subdirectories. In
addition to the functionality provided by its base class (FileSystemInfo), DirectoryInfo offers the key
members detailed in Table 20-3.
Table 20-3. Key Members of the DirectoryInfo Type
Member
Meaning in Life
Create()
CreateSubdirectory()
Create a directory (or set of subdirectories) when given a path name.
Delete()
Deletes a directory and all its contents.
GetDirectories()
Returns an array of DirectoryInfo objects that represent all subdirectories
in the current directory.
GetFiles()
Retrieves an array of FileInfo objects that represent a set of files in the
given directory.
MoveTo()
Moves a directory and its contents to a new path.
Parent
Retrieves the parent directory of this directory.
Root
Gets the root portion of a path.
You begin working with the DirectoryInfo type by specifying a particular directory path as a
constructor parameter. Use the dot (.) notation if you want to obtain access to the current working
directory (the directory of the executing application). Here are some examples:
// Bind to the current working directory.
DirectoryInfo dir1 = new DirectoryInfo(".");
756