How to: Get Information About a File in Visual Basic

The GetFileInfo method can be used to easily determine information about a file's properties. Properties of the FileInfo object include attributes, creation time, directory, directory name, whether it exists, extension, full name, last access time, last write time, length, and name.

An exception is not thrown if the file does not exist; rather, it is thrown the first time the object's properties are accessed.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

Procedure

To get information about a file

  1. Use the GetFileInfo method to retrieve a FileInfo object that can be examined to determine its properties. The following example retrieves a FileInfo object for the file MyLogFile.log.

    Dim information As System.IO.FileInfo
    information = My.Computer.FileSystem.GetFileInfo("C:\MyLogFile.log")
    
  2. Examine the FileInfo object to extract the information you need. The following lines of code report the file's full name, last access time, and length.

    MsgBox("The file's full name is " & information.FullName & ".")
    MsgBox("Last access time is " & information.LastAccessTime & ".")
    MsgBox("The length is " & information.Length & ".")
    

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

Walkthrough: Manipulating Files and Directories in Visual Basic

Reference

FileInfo

Other Resources

File Access with Visual Basic