You need to access a lot of information about a file, such as its size, its last modification time, and its attributes.
Sample code folder: Chapter 12\FileInformation
Use the My.Computer. FileSystem. GetFileInfo() method to retrieve many basic details about a specific file.
The following method displays the size, relevant dates, and attributes of a file path:
Public Sub ShowFileDetails(ByVal filePath As String) ' ----- Given a file path, show some of its details. Dim fileDetail As IO.FileInfo ' ----- First, make sure the file exists. If (My.Computer.FileSystem.FileExists(filePath) _ = False) Then MsgBox("The file '" & filePath & "' does not exist.", _ MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, _ "Invalid File") Exit Sub End If ' ----- Retrieve the file details. fileDetail = My.Computer.FileSystem.GetFileInfo(filePath) ' ----- Show some information. MsgBox("Details for '" & filePath & "':" & _ vbCrLf & vbCrLf & _ "Attributes: " & fileDetail.Attributes.ToString( ) & _ vbCrLf & _ "Created: " & fileDetail.CreationTime & vbCrLf & _ "Accessed: " & fileDetail.LastAccessTime & vbCrLf & _ "Modified: " & fileDetail.LastWriteTime & vbCrLf & _ "Size: " & fileDetail.Length & " byte(s)", _ MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, _ "File Details") End Sub
Figure 12-12 shows some typical output for this block of code.
Figure 12-12. File attributes for the Notepad.exe program file
The System.IO. FileInfo object exposes properties that document various features of a file. It also includes methods that let you create, modify, and delete the file, and open the file to examine its contents. Features such as these are discussed in other recipes found throughout this chapter.
A similar detail-laden object exists for directories. Once you have a directory path, use the My.Computer. FileSystem.GetDirectoryInfo() method, which returns an object of type System.IO.DirectoryInfo.
Some of the properties of the FileInfo object, such as the modification (last write) time, appear in the Details view of the Windows File Explorer. One part of that view that isn't directly available through FileInfo is the Type column. This displays a short name for the type of file based on its extension; for example, the .bmp extension equates to a file type of "Bitmap Image." To get this type name, you need to access values in the system registry. The sample code in this discussion uses the registry features found in the My namespace without much explanation. For additional information on using these registry features, see Recipe 14.20.
The registry consists of several "hives," one of which is HKEY_CLASSES_ROOT. This hive contains a key for each file extension recognized by Microsoft Windows. The "default value" for that key refers to another key in the same hive, and the default value for that second key will finally give us the name we seek.
The following function extracts the file-type name from the registry. The argument passed must be the valid name of an existing file:
Public Function GetFileTypeName( _ ByVal filepath As String) As String ' ----- Given a file path, obtain its file type. Dim fileDetail As IO.FileInfo Dim oneKey As Microsoft.Win32.RegistryKey Dim valueText As String ' ----- First, make sure the file exists. If (My.Computer. FileSystem.FileExists(filepath) _ = False) Then MsgBox("The file '" & filepath & "' does not exist.", _ MsgBoxStyle.OkOnly Or MsgBoxStyle.Exclamation, _ "Invalid File") Return "" End If ' ----- Retrieve the file details. fileDetail = My.Computer.FileSystem.GetFileInfo(filepath) If (fileDetail.Extension Is Nothing) Then Return "" If (fileDetail.Extension = "") Then Return "" ' ----- Access the extension's entry in the registry. oneKey = My.Computer.Registry.ClassesRoot.OpenSubKey( _ fileDetail.Extension) valueText = oneKey.GetValue("") oneKey.Close( ) If (valueText Is Nothing) Then Return "" If (valueText = "") Then Return "" ' ----- Access the extension type's entry in the registry. oneKey = My.Computer.Registry.ClassesRoot.OpenSubKey( _ valueText) valueText = oneKey.GetValue("") oneKey.Close( ) If (valueText Is Nothing) Then valueText = "" Return valueText End Function
Recipe 12.10 shows how to determine if a specified file exists.