如何:查询具有指定属性或名称的文件 - Visual Basic

如何:查询具有指定属性或名称的文件 - Visual Basic

此示例演示如何查找指定目录树中具有指定文件扩展名(例如“.txt”)的所有文件。 它还显示如何基于创建时间返回树中最新或最早的文件。

示例:

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

编译代码

创建 Visual Basic 控制台应用程序项目,其中包含 Imports System.Linq 命名空间的语句。

另请参阅

LINQ to Objects(Visual Basic)

LINQ 和文件目录 (Visual Basic)

相关推荐

web端如何做到保持登陆状态
365bet官方网址

web端如何做到保持登陆状态

📅 08-09 👀 7376
为什么游戏厂商们越来越喜欢“炒冷饭”?
365bet官方网址

为什么游戏厂商们越来越喜欢“炒冷饭”?

📅 01-29 👀 9785
有鸡有狗的成语
亚洲365bet备用

有鸡有狗的成语

📅 07-01 👀 3960
成语大全搜索
beat365亚洲体育在线

成语大全搜索

📅 02-21 👀 9873
巴比倫河(中東地區河流)
beat365亚洲体育在线

巴比倫河(中東地區河流)

📅 10-05 👀 7774
香港猪骨煲
亚洲365bet备用

香港猪骨煲

📅 01-04 👀 1752
猫咪难产怎么办?这些紧急措施你必须知道
亚洲365bet备用

猫咪难产怎么办?这些紧急措施你必须知道

📅 08-28 👀 7247
[国际足球]《你好,世界杯》第十期
亚洲365bet备用

[国际足球]《你好,世界杯》第十期

📅 07-18 👀 783
小红书上兼职靠谱吗?深度解析平台兼职类型、避坑指南与真实案例