Get File and Directory Information

In VB 2005, you can access all the file and directory information you need from a single starting point: the new My.Computer.FileSystem object.

Note

The new My.Computer. FileSystem object lets you get file and directory information with a bare minimum of code.

How do I do that?

Here are four key methods of My.Computer.FileSystem that you can use to get file and directory information. Every method has the same signature—it takes a single string parameter whose value is the complete path of the file or directory that's the subject of your query. The methods are:

FileExists( )

Returns True if the file exists.

DirectoryExists( )

Returns True if the directory exists.

GetFileInfo( )

Returns a FileInfo object. You can examine its various properties to get information such as file size, attributes, and so on.

GetDirectoryInfo( )

Returns a DirectoryInfo object. You can examine its various properties to get information such as directory size, attributes, and so on.

The code snippet shown in Example 5-2 first determines whether a file exists and then displays some information when it does.

Example 5-2. Retrieving information about a specific file

Imports System.IO Module FileInfoTest Public Sub Main( ) ' Get a file in a "special directory." Dim Info As FileInfo Info = My.Computer.FileSystem.GetFileInfo("c:\Windows\explorer.exe") ' Show the access/update times. Console.WriteLine("Created: " & Info.CreationTime) Console.WriteLine("Last Modified: " & Info.LastWriteTime) ...

Get Visual Basic 2005: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.