The IO namespace provides three objects that represent drives, folders, and files: the DriveInfo, DirectoryInfo, and FileInfo classes. These classes, in turn, expose a number of basic properties of the entities they represent. Notice that they’re instance objects, and you must create a new instance of the corresponding class by specifying the name of a drive/folder/file in its constructor. The same three objects are returned by the GetDriveInfo, GetDirectoryInfo and GetFileInfo methods of the FileSystem object.
The DriveInfo Class
The DriveInfo class provides basic information about a drive. Its constructor accepts as an argument a drive name, and you can use the object returned by the method to retrieve information about the specific drive, as shown here:
Dim Drive As New DriveInfo("C")
Code language: PHP (php)
The argument is the name of a drive (you can include the colon, if you want). Notice that you can’t specify a Universal Naming Convention (UNC) path with the constructor of the DriveInfo object. You can only access local drives or network drives that have been mapped to a drive name on the target system.
To retrieve information about the specified drive, use the following properties of the DriveInfo class:
DriveFormat: A string describing the drive’s format (FAT32, NTFS).
DriveType: A string describing the drive’s type (fixed, CD-ROM, and so on).
TotalSize: The drive’s total capacity, in bytes.
TotalFreeSize: The total free space on the drive, in bytes.
AvailableFreeSpace: The available free space on the drive, in bytes.
VolumeLabel: The drive’s label. You can change the drive’s label by setting this property.
IsReady: A True/False value indicating whether the drive is ready to be used. Retrieve this property’s setting before calling any of the other properties to make sure that you’re not attempting to access an empty floppy or CD drive.
Discovering the System’s Drives
The DriveInfo class exposes the GetDrives method, which returns an array of DriveInfo objects, one for each drive on the system. This method is similar to the GetLogicalDrives method of the Directory object, which is a shared method and doesn’t require that you create an object explicitly.
The DirectoryInfo Class
To create a new instance of the DirectoryInfo class that references a specific folder, supply the folder’s path in the class’s constructor:
Dim DI As New DirectoryInfo(path)
Code language: PHP (php)
The members of the DirectoryInfo class are equivalent to the members of the Directory class, and you will recognize them as soon as you see them in the IntelliSense drop-down list. Here are a couple of methods that are unique to the DirectoryInfo class.
CreateSubdirectory
This method creates a subfolder under the folder specified by the current instance of the class, and its syntax is as follows:
DI.CreateSubdirectory(path)
Code language: CSS (css)
The CreateSubdirectory method returns a DirectoryInfo object that represents the new subfolder. The path argument need not be a single folder’s name. If you specified multiple nested folders, the CreateSubdirectory method will create the appropriate hierarchy, similar to the CreateDirectory method of the Directory class.
GetFileSystemInfos
This method returns an array of FileSystemInfo objects, one for each item in the folder referenced by the current instance of the class. The items can be either folders or files. To retrieve information about all the entries in a folder, create an instance of the DirectoryInfo class and then call its GetFileSystemInfos method:
Dim DI As New DirectoryInfo(path)
Dim itemsInfo() As FileSystemInfo
itemsInfo = DI.GetFileSystemInfos()
Code language: PHP (php)
You can also specify an optional search pattern as an argument when you call this method:
itemsInfo = DI.GetFileSystemInfos(pattern)
The FileSystemInfo objects expose a few properties, which are not new to you. The Name, FullName, and Extension properties return a file’s or folder’s name, or full path, or a file’s extension,respectively. CreationTime, LastAccessTime, and LastWriteTime are also properties of the FileSystemInfo object, as well as the Attributes property.
You will notice that there are no properties that determine whether the current item is a folder or a file. To find out the type of an item, use the Directory member of the Attributes property:
If itemsInfo(i).Attributes And FileAttributes.Directory Then
{ current item is a folder }
Else
{ current item is a file }
End If
Code language: PHP (php)
The code in Listing 11.6 retrieves all the items in the C:\Program Files folder and prints their names along with the FOLDER or FILE characterization.
Listing 11.6: Processing a Folder’s Items with the FileSystemInfo Object
Dim path As String = "C:\Program Files"
Dim DI As New DirectoryInfo(path)
Dim itemsInfo() As FileSystemInfo
itemsInfo = DI.GetFileSystemInfos()
Dim item As FileSystemInfo
For Each item In itemsInfo
If (item.Attributes And FileAttributes.Directory)= _
FileAttributes.Directory Then
Debug.Write("FOLDER ")
Else
Debug.Write("FILE ")
End If
Debug.WriteLine(item.Name)
Next
Code language: PHP (php)
Notice the differences between the GetFileSystemInfos method of the DirectoryInfo class and the GetFileSystemEntries of the Directory object. GetFileSystemInfos returns an array of objects that contains information about the current item (file or folder). GetFileSystemEntries returns an array of strings (the names of the folders and files).
The FileInfo Class
The FileInfo class exposes many properties and methods, which are equivalent to the members of the File class, so I’m not going to repeat all of them here. The Copy/Delete/Move methods allow you to manipulate the file represented by the current instance of the FileInfo class, similar to the methods by the same name of the File class. Although there’s substantial overlap between the members of the FileInfo and File classes, the difference is that with FileInfo you don’t have to specify a path; its members act on the file represented by the current instance of the FileInfo class, and this file is passed as an argument to the constructor of the FileInfo class. The FileInfo class exposes a few rather trivial properties, which are mentioned briefly here.
Length Property
This property returns the size of the file represented by the FileInfo object in bytes. The File class doesn’t provide an equivalent property or method.
CreationTime, LastAccessTime, LastWriteTime Properties
These properties return a date value, which is the date the file was created, accessed for the last time, or written to for the last time, respectively. They are equivalent to the methods of the File object by the same name and the Get prefix.
Name, FullName, Extension Properties
These properties return the filename, full path, and extension, respectively, of the file represented by the current instance of the FileInfo class. They have no equivalents in the File class because the File class’s methods require that you specify the path of the file, so its path and extension are known.
CopyTo, MoveTo Methods
These two methods copy or move, respectively, the file represented by the current instance of the FileInfo class. Both methods accept a single argument, which is the destination of the operation (the path to which the file will be copied or moved). If the destination file exists already, you can overwrite it by specifying a second optional argument, which has a True/False value:
FileInfo.CopyTo(path, force)
Code language: CSS (css)
Both methods return an instance of the FileInfo class, which represents the new file — if the operation completed successfully.
Directory Method
This method returns a DirectoryInfo value that contains information about the file’s parent directory.
DirectoryName Method
This method returns a string with the name of the file’s parent directory. The following statements return the two (identical) strings shown highlighted in this code segment:
Dim FI As FileInfo
FI = New FileInfo("c:\folder1\folder2\folder3\test.txt")
Debug.WriteLine(FI.Directory().FullName)
c:\folder1\folder2\folder3
Debug.WriteLine(FI.DirectoryName()) c:\folder1\folder2\folder3
Code language: PHP (php)
Of course, the Directory method returns an object, which you can use to retrieve other properties of the parent folder.