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
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")
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:
The path name is malformed. For example, it contains invalid characters or is only white space (ArgumentException).
The file does not exist or is Nothing (ArgumentNullException).
The path contains a colon in the middle of the string (NotSupportedException).
The path is too long (PathTooLongException).
The user lacks necessary permissions (SecurityException).
The user lacks ACL (access control list) access to the file (UnauthorizedAccessException).
See Also
Tasks
Walkthrough: Manipulating Files and Directories in Visual Basic