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.

Return Value

Read-only collection of type String.

Exceptions

The following conditions may cause an exception:

Remarks

An empty collection is returned if no files matching the specified pattern are found.

Tasks

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

Example

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.

Requirements

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

Permissions

The following permission may be necessary:

Permission Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

Walkthrough: Manipulating Files and Directories in Visual Basic

Reference

My.Computer.FileSystem Object
SearchOption Enumeration
System.Collections.ObjectModel.ReadOnlyCollection
Microsoft.VisualBasic.FileIO.FileSystem.FindInFiles