Section 5.2. Get File and Directory Information


5.2. 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.

5.2.1. 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 signatureit 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)         Console.WriteLine("Last Accessed: " & Info.LastAccessTime)              ' Check if the file is read-only. When testing file attributes,         ' you need to use bitwise arithmetic, because the FileAttributes         ' collection usually contains more than one attribute at a time.         Dim ReadOnlyFile As Boolean         ReadOnlyFile = Info.Attributes And FileAttributes.ReadOnly         Console.WriteLine("Read-Only: " & ReadOnlyFile)              ' Show the size.         Console.WriteLine("Size (bytes): " & Info.Length)     End Sub      End Module

Here's the type of output you'll see:

Created: 3/30/2004 7:35:17 PM Last Modified: 8/29/2002 4:41:24 AM Last Accessed: 4/28/2004 10:59:38 AM Read-Only: False Size (bytes): 104032 Version: 6.0.1106

5.2.2. What about...

...searching for directories and files? The My.Computer.FileSystem object also provides a GeTDirectories( ) method to retrieve the names of all the subdirectories in a directory and a GetFiles() method to retrieve the names of all files in a given directory.


Note: In early beta versions, Visual Basic included new FolderProperties and FileProperties classes that duplicated the DirectoryInfo and FileInfo classes. Fortunately, Microsoft decided not to reinvent the wheel, and went back to the . NET 1.x standards.

Both methods offer additional flexibility via an overloaded version that accepts additional parameters. You can specify an array with one or more filter strings (for example, use *.doc to find all the files with the extension .doc). You can also supply a Boolean includeSubFolders parameter that, if TRue, searches for matching files or directories in every contained subdirectory.

Here's an example of an advanced search that finds all the .exe files in the c:\windows directory:

' Get all the EXE files in the Windows directory. For Each File As String In My.Computer.FileSystem.GetFiles( _   "c:\windows\", True, "*.exe")     Info = My.Computer.FileSystem.GetFileInfo(File)     Console.WriteLine(Info.Name & " in " & Info.Directory.Name) Next

Note that the GetFiles( ) and Getdirectories( ) methods just return strings. If you want more information, you need to create a FileInfo or DirectoryInfo object for the file or directory, as shown above.

There is one caveat: when you perform a search with the GetFiles( ) method, the matching file list is first created and then returned to your code. In other words, if you're performing a time-consuming search, you won't receive a single result until the entire search is finished.



Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net