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
Trueif the file exists.DirectoryExists( )Returns
Trueif the directory exists.GetFileInfo( )Returns a
FileInfoobject. You can examine its various properties to get information such as file size, attributes, and so on.GetDirectoryInfo( )Returns a
DirectoryInfoobject. 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) ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access