Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FileSystem.FindInFiles Method (String, String, Boolean, SearchOption)

Returns a read-only collection of strings representing the names of files containing the specified text.

Namespace:  Microsoft.VisualBasic.FileIO
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..
ExceptionCondition
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 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 In list
    ListBox1.Items.Add(name)
Next


In order to work, the project must contain a ListBox named ListBox1.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.