我有两个过程,一个过程接收数据并将其保存到特定文件夹中的文件中。 第二个进程每秒检查该文件夹中是否有新文件,将其读取并删除,但是第二个进程会不时尝试在创建文件时打开文件,并获取和异常;所以我一直在考虑只打开至少5秒旧的文件。 目前,我正在使用此代码。
Dim FilePaths As String() = Directory.GetFiles(ReportPath, "Report*.rpt", SearchOption.TopDirectoryOnly)
请注意,您可以使用此代码获取文件创建时间
File.GetCreationTime(FilePath))
但是我不确定是否可以在没有相同异常的情况下获得文件创建时间。
任何想法如何解决这个问题? 也许以另一种方式。
答案 0 :(得分:0)
尝试一下, 顺便说一句,我使用了Linqpad,它使测试速度更快。
Dim root As String = "C:\linqpad"
'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 .rpt 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 = ".rpt"
Order By file.Length _
Select file.FullName,file.CreationTime
查看linqpad使用中的输出
fileQuery.dump()
但这在Visual Studio中无法转换为任何内容