H o m e

 

S e a r c h    F o r    a    F i l e    U s i n g    a    W i l d C a r d    

By Michael McIntyre

mikemc@getdotnetcode.com

 

Most experienced Windows users know how to use the standard wild card characters ( ? and * ) to search for file(s) on their computer.  For example *.TXT would be used to find all the files with a file extension of TXT.

 

A .NET application can programmatically scan a directory for files using the standard wild cards too.  Here is an example:

 

' Declare a variable of type FileInfo named aFileInfo.

Dim aFileInfo As System.IO.FileInfo

' Declare a variable of type FileInfo Array named FileInfos.

Dim fileInfos() As System.IO.FileInfo

 

' Declare a variable of type DirectoryInfo named aDirectoryInfo.

Dim aDirectoryInfo As System.IO.DirectoryInfo

' Use the New constructor to create a DirectoryInfo object

' passing in the path to the C drive.

' Assign the result to the aDirectoryInfo variable.

aDirectoryInfo = New System.IO.DirectoryInfo("C:\")

 

' Call GetFiles function of the DirectoryInfo object

' passing in some standard wild card characters.

' Assign the result to the fileInfos array variable.

fileInfos = aDirectoryInfo.GetFiles("*.TXT")

 

' Display the names of the files found.

For Each aFileInfo In fileInfos

    Console.WriteLine(aFileInfo.Name)

Next

 

RESULT:

 

BOOTLOG.TXT

DemoAttributes.txt

 

Copyright © 2001-2003 aZ Software Developers. All rights reserved.