How to: Get Information About a File in Visual Basic
The My.Computer.FileSystem.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
The options available in dialog boxes, and the names and locations of menu commands you see, might differ from what is described in Help, depending on your active settings or edition. This Help page was written with General Development Settings in mind. To change your settings, choose Import and Export Settings on the Tools menu. 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