The DirectoryInfo and FileInfo classes mirror the functionality in the Directory and File classes. In addition, they make it easy to walk through directory and file relationships. For example, you can easily retrieve the FileInfo objects for the files in a directory represented by a DirectoryInfo object.
Note that while the Directory and File classes expose only methods, DirectoryInfo and FileInfo provide a combination of properties and methods. For example, while the File class had separate GetAttributes() and SetAttributes() methods, the FileInfo class includes an Attributes property.
Another nice thing about the DirectoryInfo and FileInfo classes is that they share a common set of properties and methods because they derive from the common FileSystemInfo base class. Table 4 describes the members they have in common.
In addition, the FileInfo and DirectoryInfo classes have a few unique members, as indicated in Table 5 and Table 6.
When you create a DirectoryInfo or FileInfo object, you specify the full path in the constructor:
Dim myDirectory As New DirectoryInfo("c:\Temp") Dim myFile As New FileInfo("c:\Temp\readme.txt")
This path may or may not correspond to a real physical file or directory. If it doesn't, you can always use the Create() method to create the corresponding file or directory:
' Define the new directory and file. Dim myDirectory As New DirectoryInfo("c:\Temp\Test") Dim myFile As New FileInfo("c:\Temp\Test\readme.txt") ' Now create them. Order here is important. ' You can't create a file in a directory that doesn't exist yet. myDirectory.Create() myFile.Create()
The DriveInfo class allows you to retrieve information about a drive on your computer. Just a few pieces of information will interest you. Typically, the DriveInfo class is merely used to retrieve the total amount of used and free space.
Table 7 shows the DriveInfo members. Unlike the FileInfo and DriveInfo classes, there's no Drive class with instance versions of these methods.
Attempting to read from a drive that's not ready (for example, a CD drive that doesn't have a CD in it) will throw an exception. To avoid this problem, check the DriveInfo.IsReady property, and attempt to read other properties only if it returns True.
You can use methods such as DirectoryInfo.GetFiles() and DirectoryInfo.GetDirectories() to create a simple file browser. The following example shows you how. Be warned that, although this code is a good example of how to use the DirectoryInfo and FileInfo classes, it isn't a good example of security. Generally, you wouldn't want a user to be able to find out so much information about the files on your web server.
The sample file browser program allows the user to see information about any file in any directory in the current drive, as shown in Figure 2.
The code for the file browser page is as follows:
Public Partial Class FileBrowser Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Dim startingDir As String = "c:\" lblCurrentDir.Text = startingDir ShowFilesIn(startingDir) ShowDirectoriesIn(startingDir) End If End Sub Private Sub ShowFilesIn(ByVal dir As String) lblFileInfo.Text = ""
lstFiles.Items.Clear() Try Dim dirInfo As New DirectoryInfo(dir) For Each fileItem As FileInfo In dirInfo.GetFiles() lstFiles.Items.Add(fileItem.Name) Next Catch err As Exception ' Ignore the error and leave the list box empty. End Try End Sub Private Sub ShowDirectoriesIn(ByVal dir As String) lstDirs.Items.Clear() Try Dim dirInfo As New DirectoryInfo(dir) For Each dirItem As DirectoryInfo In dirInfo.GetDirectories() lstDirs.Items.Add(dirItem.Name) Next Catch err As Exception ' Ignore the error and leave the list box empty. End Try End Sub Protected Sub cmdBrowse_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles cmdBrowse.Click ' Browse to the currently selected subdirectory. If lstDirs.SelectedIndex <> −1 Then Dim newDir As String = Path.Combine(lblCurrentDir.Text, _ lstDirs.SelectedItem.Text) lblCurrentDir.Text = newDir ShowFilesIn(newDir) ShowDirectoriesIn(newDir) End If End Sub Protected Sub cmdParent_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles cmdParent.Click ' Browse up to the current directory's parent. ' The Directory.GetParent method helps us out. Dim newDir As String If Directory.GetParent(lblCurrentDir.Text) Is Nothing Then ' This is the root directory; there are no more levels. Exit Sub Else newDir = Directory.GetParent(lblCurrentDir.Text).FullName End If lblCurrentDir.Text = newDir ShowFilesIn(newDir) ShowDirectoriesIn(newDir) End Sub
Protected Sub cmdShowInfo_Click(ByVal sender As Object, _ ByVal e As EventArgs) Handles cmdShowInfo.Click ' Show information for the currently selected file. If lstFiles.SelectedIndex <> −1 Then Dim fileName As String = Path.Combine(lblCurrentDir.Text, _ lstFiles.SelectedItem.Text) Dim displayText As New StringBuilder() Try Dim selectedFile As New FileInfo(fileName) displayText.Append("<b>") displayText.Append(selectedFile.Name) displayText.Append("</b><br />Size: ") displayText.Append(selectedFile.Length) displayText.Append("<br />") displayText.Append("Created: ") displayText.Append(selectedFile.CreationTime.ToString()) displayText.Append("<br />Last Accessed: ") displayText.Append(selectedFile.LastAccessTime.ToString()) Catch err As Exception displayText.Append(err.Message) End Try lblFileInfo.Text = displayText.ToString() End If End Sub End Class
The list controls in this example don't post back immediately. Instead, the web page relies on the Browse to Selected, Up One Level, and Show Info buttons.
By default, directory names don't end with a trailing backslash (\) character (for example, c:\Temp is used instead of c:\Temp\). However, when referring to the root drive, a slash is required. This is because of an interesting inconsistency that dates back to the days of DOS. When using directory names, c:\ refers to the root drive, but c: refers to the current directory, whatever it may be. This quirk can cause problems when you're manipulating strings that contain file names, because you don't want to add an extra trailing slash to a path (as in the invalid path c:\\myfile.txt). To solve this problem, the page uses the Combine() method of the Path class. This method correctly joins any file and path name together, adding the \ when required.
The code includes all the necessary error handling code. If you attempt to read the information for a file that you aren't permitted to examine, the error message is displayed instead of the file details section. If an error occurs when calling DirectoryInfo.GetFiles() or DirectoryInfo.GetDirectories(), the error is simply ignored and the files or subdirectories aren't shown. This error occurs if the account that's running your code doesn't have permission to read the contents of the directory. For example, this occurs if you try to access the c:\System Volume Information directory in Windows and you're not an administrator.
The ShowFilesIn() and ShowDirectoriesIn() methods loop through the file and directory collections to build the lists. Another approach is to use data binding instead, as shown in the following code sample:
' Another way to fill lstFiles. Dim dirInfo As New DirectoryInfo(dir) lstFiles.DataSource = dirInfo.GetFiles() lstFiles.DataMember = "Name" lstFiles.DataBind()
Just remember that when you bind a collection of objects, you need to specify which property will be used for the list. In this case, it's the DirectoryInfo.Name or FileInfo.Name property.