
354
LESSON 30 Using File system Classes
PROPERTY PURPOSE
IsReadOnly
Returns true if the file is marked read-only.
LastAccessTime
The time at which the file was last accessed.
LastWriteTime
The time at which the file was last written.
Length
The file’s size in bytes.
Name
The file’s name without the path.
Example program UseFileInfo (found in this lesson’s code download) uses a FileInfo object to
display information about files.
THE FILE CLASS
The File class provides static methods for manipulating files (see Table 30-7). For simple tasks
these are sometimes easier to use than the comparable
FileInfo class methods because you don’t
need to create a
FileInfo object to use them. The AppendAllText, ReadAllLines, ReadAllText,
WriteAllLines, and WriteAllText methods are particularly useful for reading and writing text
files all at once, although you may still want to use
StreamReader and StreamWriter if you need
to manipulate files one line at a time.
TABLE 307
METHOD PURPOSE
AppendAllText
Appends a string to the end of a file.
Copy
Copies a file to a new file.
Create
Creates a file.
Decrypt
Decrypts a file that was encrypted by the Encrypt method.
Delete
Deletes a file.
Encrypt
Encrypts the file so it can only be read by the account used to encrypt it.
Exists
Returns true if a file exists.
GetAttributes
Returns a file’s attributes, such as ReadOnly, System, or Hidden.
GetCreationTime
Returns the time at which the file was created. ...