My.Computer.FileSystem.FindInFiles Method
Returns a read-only collection of strings representing the names of files containing the specified text.
' Usage Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.FindInFiles(directory ,containsText ,ignoreCase ,searchType) Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.FindInFiles(directory ,containsText ,ignoreCase ,searchType ,fileWildcards) ' Declaration Public Function FindInFiles( _ ByVal directory As String, _ ByVal containsText As String, _ ByVal ignoreCase As Boolean, _ ByVal searchType As SearchOption _ ) As System.Collections.ObjectModel.ReadOnlyCollection(Of String) ' -or- Public Function FindInFiles( _ ByVal directory As String, _ ByVal containsText As String, _ ByVal ignoreCase As Boolean, _ ByVal searchType As SearchOption, _ ByVal fileWildcards As String() _ ) As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Parameters
- directory
-
String. The directory to be searched. Required.
- containsText
-
String. The search text. Required.
- ignoreCase
-
Boolean. Whether the search should be case-sensitive or not. Default is True. Required.
- searchType
-
SearchOption Enumeration. Whether to include subfolders. Default is SearchOption.SearchTopLevelOnly. Required.
- fileWildcards
-
Array of type String. Pattern to be matched. Required.
The following conditions may cause an exception:
-
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 \\.\) (ArgumentException).
-
The path is not valid because it is Nothing (ArgumentNullException).
-
directory does not exist (DirectoryNotFoundException).
-
directory points to an existing file (IOException).
-
The path exceeds the system-defined maximum length (PathTooLongException).
-
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
-
The user lacks necessary permissions to view the path (SecurityException).
-
The user lacks necessary permissions (UnauthorizedAccessException).
The following table lists examples of tasks 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 As String In list ListBox1.Items.Add(name) Next
In order to work, the project must contain a ListBox named ListBox1.
Namespace: Microsoft.VisualBasic.MyServices
Class: FileSystemProxy (provides access to FileSystem)
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Availability by Project Type
| Project type | Available |
|---|---|
| Windows Application | Yes |
| Class Library | Yes |
| Console Application | Yes |
| Windows Control Library | Yes |
| Web Control Library | Yes |
| Windows Service | Yes |
| Web Site | Yes |
The following permission may be necessary:
| Permission | Description |
|---|---|
| Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
For more information, see Code Access Security and Requesting Permissions.