폴더의 파일 내용을 쿼리하는 방법(LINQ)(Visual Basic)
이 예제에서는 지정된 디렉터리 트리에 있는 모든 파일을 쿼리하고 각 파일을 연 다음 내용을 검사하는 방법을 보여 줍니다. 이러한 유형의 기술을 사용하여 디렉터리 트리 내용의 인덱스 또는 역방향 인덱스를 만들 수 있습니다. 이 예제에서는 단순 문자열 검색이 수행됩니다. 그러나 정규식을 사용하면 더 복잡한 유형의 패턴 일치를 수행할 수 있습니다. 자세한 내용은 방법: LINQ 쿼리와 정규식 결합(Visual Basic)을 참조하세요.
예시
Imports System.IO
Module Module1
'QueryContents
Public Sub Main()
' Modify this path as necessary.
Dim startFolder = "C:\Program Files (x86)\Microsoft Visual Studio 14.0"
' Take a snapshot of the folder contents.
Dim dir As New DirectoryInfo(startFolder)
Dim fileList = dir.GetFiles("*.*", SearchOption.AllDirectories)
Dim searchTerm = "Welcome"
' Search the contents of each file.
' A regular expression created with the Regex class
' could be used instead of the Contains method.
Dim queryMatchingFiles = From file In fileList _
Where file.Extension = ".html" _
Let fileText = GetFileText(file.FullName) _
Where fileText.Contains(searchTerm) _
Select file.FullName
Console.WriteLine("The term " & searchTerm & " was found in:")
' Execute the query.
For Each filename In queryMatchingFiles
Console.WriteLine(filename)
Next
' Keep the console window open in debug mode.
Console.WriteLine("Press any key to exit")
Console.ReadKey()
End Sub
' Read the contents of the file. This is done in a separate
' function in order to handle potential file system errors.
Function GetFileText(name As String) As String
' If the file has been deleted, the right thing
' to do in this case is return an empty string.
Dim fileContents = String.Empty
' If the file has been deleted since we took
' the snapshot, ignore it and return the empty string.
If File.Exists(name) Then
fileContents = File.ReadAllText(name)
End If
Return fileContents
End Function
End Module
코드 컴파일
Visual Basic 콘솔 애플리케이션 프로젝트를 만들고, 코드 샘플을 복사하여 붙여넣고, 프로젝트 속성에서 시작 개체 값을 조정합니다.
참고 항목
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET