Figure 2.8
An attractive error message is displayed if the user enters an invalid directory name.
A useful property of the DirectoryInfo class (and the FileInfo class, which we'll examine in the next section, "Reading, Writing, and Creating Files") that deserves further attention is the Attributes property. This property is of type FileAttributes, an enumeration also found in the System.IO namespace. The FileAttributes enumeration lists the various attributes a directory (or file) can have. Table 2.1 lists these attributes.
Table 2.1 Available Attributes in the FileAttributes Enumeration
Attribute
Description
Archive
Indicates the file system entity's archive status
Compressed
Indicates the file system entity's compression status
Directory
Indicates if the file system entity is a directory
Encrypted
Indicates whether the file system entity is encrypted
Hidden
Indicates if the file system entity is hidden
Normal
If the file system entity has no other attributes set, it is labeled as Normal
NotContentIndexed
Indicates whether the file system entity will be indexed by the operating system's indexing service
Offline
Indicates if the file system entity is offline
ReadOnly
Indicates whether the file system entity is read-only
ReparsePoint
Indicates if the file system entity contains a reparse point (a block of user-defined data)
SparseFile
Indicates if a file is defined as a sparse file
System
Indicates if the file is a system file
Temporary
Indicates whether the file system entity is temporary or not
Because each directory (or file) can have a number of attributes (such as a file being both hidden and a system file), the single Attributes property has the capability of housing multiple pieces of information. To pick out the individual attributes represented by the Attributes property, a bit-wise AND (BitAnd) can be used (see lines 48 through 61). To properly display the attributes for a directory in Listing 2.2.1, a helper function, DisplayAttributes, is called from line 15, passing to it the FileAttributes enumeration returned by the Attributes property.
--------------------------------------------------------------------------------
Note
The bit-wise AND operator, BitAnd, is new to Visual Basic.NET. In previous versions of Visual Basic and VBScript, the AND operator served both as a logical and bit-wise AND. For a list of new features in Visual Basic.NET check out Appendix A, "Breaking Cahnges in Visual Basic.NET."
--------------------------------------------------------------------------------
The DisplayAttributes function, spanning lines 44 through 69, returns a nicely formatted display listing the various attributes indicated by the FileAttributes enumeration passed in (fsa). On lines 48 through 61, a check is performed to determine if fsa contains a particular attribute; if it does, the textual description of the attribute is appended to strOutput, which will be returned by DisplayAttributes at the end of the function.
The DirectoryInfo class contains two useful methods for retrieving a list of a directory's subdirectories and folders. These two methods are GetDirectories(), which returns an array of DirectoryInfo objects representing the subdirectories, and GetFiles(), which returns an array of FileInfo objects representing the list of files in the directory. (We'll examine the FileInfo object in detail in the next section, "Reading, Writing, and Creating Files." In lines 31 through 33,