Aracılığıyla paylaş


Nasıl yapılır: Belirtilen Öznitelik veya Ada Sahip Dosyaları Sorgulama (Visual Basic)

Bu örnekte, belirtilen bir dizin ağacında belirtilen dosya adı uzantısına (örneğin ".txt") sahip tüm dosyaların nasıl bulunduğunu gösterir. Ayrıca, oluşturma zamanına göre ağaçtaki en yeni veya en eski dosyanın nasıl döndürüleceği de gösterilir.

Örnek

Module FindFileByExtension  
    Sub Main()  
  
        ' Change the drive\path if necessary  
        Dim root As String = "C:\Program Files\Microsoft Visual Studio 9.0"  
  
        'Take a snapshot of the folder contents  
        Dim dir As New System.IO.DirectoryInfo(root)  
  
        Dim fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories)  
  
        ' This query will produce the full path for all .txt files  
        ' under the specified folder including subfolders.  
        ' It orders the list according to the file name.  
        Dim fileQuery = From file In fileList _  
                        Where file.Extension = ".txt" _  
                        Order By file.Name _  
                        Select file  
  
        For Each file In fileQuery  
            Console.WriteLine(file.FullName)  
        Next  
  
        ' Create and execute a new query by using  
        ' the previous query as a starting point.  
        ' fileQuery is not executed again until the  
        ' call to Last  
        Dim fileQuery2 = From file In fileQuery _  
                         Order By file.CreationTime _  
                         Select file.Name, file.CreationTime  
  
        ' Execute the query  
        Dim newestFile = fileQuery2.Last  
  
        Console.WriteLine("\r\nThe newest .txt file is {0}. Creation time: {1}", _  
                newestFile.Name, newestFile.CreationTime)  
  
        ' Keep the console window open in debug mode  
        Console.WriteLine("Press any key to exit.")  
        Console.ReadKey()  
  
    End Sub  
End Module  

Kodu derleme

System.Linq ad alanı için bir deyim içeren bir Imports Visual Basic konsol uygulaması projesi oluşturun.

Ayrıca bkz.