The class you'll work with most often when working with a user's file system is System.IO. This class provides functionality for working with both files and directories, and allows you to delete, move, copy, and rename files and directories using simple method calls. In addition, System.IO contains the Path class, which is extremely useful for working with paths.
Note
System.IO provides classes for creating new files and writing to existing files (both binary and text). Such files can contain just about anything you can imagine, and I won't be covering these classes in this chapter.
File operations are accomplished using System.IO.File, while directory operations are performed using (obviously enough) System.IO.Directory. Table 12-1 lists commonly used members of System.IO.File, while Table 12-2 lists commonly accessed members of System.IO.Directory.
Member | Description |
---|---|
Copy | Copies an existing file to a new file. |
Delete | Deletes the file specified by the fully qualified path. Note: No exception is thrown if the specified file does not exist. |
Exists | Determines whether the specified file exists. |
GetAttributes | Gets the FileAttributes of the file in the fully qualified path. |
GetCreationTime | Returns the creation date and time of the specified file. |
GetLastAccessTime | Returns the date and time the specified file was last accessed. |
GetLastWriteTime | Returns the date and time the specified file was last written to. |
Move | Moves a specified file to a new location, providing the option to specify a new file name. |
SetAttributes | Sets the specified FileAttributes of the file in the specified path. |
SetCreationTime | Sets the date and time the file was created. |
SetLastAccessTime | Sets the date and time the file was last accessed. |
SetLastWriteTime | Sets the date and time that the specified file was last written to. |
Member | Description |
---|---|
CreateDirectory | Creates all directories and subdirectories as specified by the provided path |
Delete | Deletes a directory and all of its contents |
Exists | Determines whether a given path refers to an existing directory on disk |
GetCreationTime | Gets the creation date and time of a directory |
GetDirectories | Gets the names of subdirectories in the specified directory |
GetDirectoryRoot | Returns volume information, root information, or both for the specified path |
GetFiles | Returns the names of files in the specified directory |
GetFilesSystemEntries | Returns the names of all files and subdirectories in the specified directory |
GetLastAccessTime | Returns the date and time the specified directory was last accessed |
GetLastWriteTime | Returns the date and time the specified directory was last written to |
GetLogicalDrives | Retrieves the names of the logical drives on the computer in the form "driveletter:\" |
GetParent | Retrieves the parent directory of the specified path, including both absolute and relative paths |
Move | Moves a file or a directory and its contents to a new location |
SetCreationTime | Sets the creation date and time for the specified directory |
SetCurrentDirectory | Sets the application's current working directory to the specified directory |
SetLastAccessTime | Sets the date and time the specified directory was last accessed |
SetLastWriteTime | Sets the date and time a directory was last written to |
Notice the similarities between members in System.IO.File and System.IO.Directory. For example, to determine whether a file exists you would use a statement like this:
blnFileExists = System.IO.File.Exists("c:\temp\test.txt")
While you would use this statement to determine whether a directory exists:
blnDirectoryExists = System.IO.Directory.Exists("c:\temp")
This type of consistency permeates the File and Directory classes, making it much easier to learn and use them both.
Caution
Neither the Delete method of System.IO.File nor that of System.IO.Directory sends deleted information to the Recycle Bin they both permanently destroy the related data.
Most of the members of System.IO.File and System.IO.Directory are pretty straightforward. Something that bears a bit of explaining, however, is retrieving properties (attributes) of a file. This is the data you see when you right-click a file in Windows Explorer and choose Properties. (See Figure 12-1.) If you just want to retrieve date and time information, System.IO.File has members that you can use. However, if you want to get additional data (such as whether a file is flagged as read-only), you have to do a little more work.
The method GetAttributes of System.IO.File is used to get the non-date-related attributes of a file. GetAttributes returns a Long, which in turn acts as a set of flags for the file's attributes. The method used to store these values is called bit packing. Bit packing is the process whereby each bit in a byte (or multiple bytes) is used to represent either an off or an on state. The numeric value of the byte is pretty much irrelevant you have to determine the state of a specific bit in order to make any sense of the value. The first step in this process is to get the Long containing the flags for the file attributes. To do this, create a Long variable and call GetAttributes, like this:
Dim lngAttributes As Long = System.IO.File.GetAttributes("c:\test.txt")
Once you have the flag bits in the Long, you can determine whether a particular attribute is set by Anding the variable with one of the flags of IO.File-Attributes shown in Table 12-3. For example, to determine whether a file's ReadOnly flag is set, you could use a statement like this:
blnReadOnly = lngAttributes And IO.FileAttributes.ReadOnly
When you And a flag value with a variable, you'll get True if the variable contains the flag; otherwise, you'll get False.
Attribute | Description |
---|---|
Archive | The file's archive status. Applications use this attribute to mark files for backup and removal. |
Directory | The file is a directory. |
Hidden | The file is hidden and therefore not included in an ordinary directory listing. |
Normal | The file is normal and has no other attributes set. |
ReadOnly | The file is a read-only file. |
System | The file is part of the operating system or is used exclusively by the operating system. |
Temporary | The file is a temporary file. |