FileSystem.FindInFiles Method (String, String, Boolean, SearchOption)
Returns a read-only collection of strings representing the names of files containing the specified text.
Assembly: Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
public static ReadOnlyCollection<string> FindInFiles( string directory, string containsText, bool ignoreCase, SearchOption searchType )
Parameters
- directory
- Type: System.String
The directory to be searched.
- containsText
- Type: System.String
The search text.
- ignoreCase
- Type: System.Boolean
True if the search should be case-sensitive; otherwise False. Default is True.
- searchType
- Type: Microsoft.VisualBasic.FileIO.SearchOption
Whether to include subfolders. Default is SearchOption.SearchTopLevelOnly.
Return Value
Type: System.Collections.ObjectModel.ReadOnlyCollection<String>Read-only collection of the names of files containing the specified text..
| Exception | Condition |
|---|---|
| ArgumentException | The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\). |
| ArgumentNullException | directory is Nothing or an empty string. |
| DirectoryNotFoundException | The specified directory does not exist. |
| IOException | The specified directory points to an existing file. |
| PathTooLongException | The path exceeds the system-defined maximum length. |
| NotSupportedException | The specified directory path contains a colon (:) or is in an invalid format. |
| SecurityException | The user lacks necessary permissions to view the path. |
| UnauthorizedAccessException | The user lacks necessary permissions. |
An empty collection is returned if no files matching the specified pattern are found.
The following table lists an example of a task involving the My.Computer.FileSystem.FindInFiles method.
To | See |
Search a directory for files containing a specific string | Walkthrough: Manipulating Files and Directories in Visual Basic |
This example searches the directory C:\TestDir for any files containing the string "sample string" and displays the results in ListBox1.
Dim list As System.Collections.ObjectModel. ReadOnlyCollection(Of String) list = My.Computer.FileSystem.FindInFiles("C:\TestDir", "sample string", True, FileIO.SearchOption.SearchTopLevelOnly) For Each name In list ListBox1.Items.Add(name) Next
In order to work, the project must contain a ListBox named ListBox1.
- FileIOPermission
Controls the ability to access files and folders. Associated enumeration: Unrestricted.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.