gpt4 book ai didi

vb.net - 如何按日期/时间过滤文件名?

转载 作者:行者123 更新时间:2023-12-04 18:08:06 25 4
gpt4 key购买 nike

我需要帮助在我的程序的报告生成(运动检测系统)中过滤特定时间/日期(所有文件都是 .jpeg 格式),用户可以在其中查看从特定时间点到另一个(例如下午 1:00 - 2:00pm) 然后在列表框中显示文件。

enter image description here
示例截图 文件名:pic_HHMMss_ddMMMyyyy

系统是这样工作的。网络摄像头检测到运动后,它会自动捕获图像并将其保存到 C:\Surveillance System\Detected 并生成文件名 pic_HHMMSs_ddMMMyyyy。所以这是现在的报告生成表格,其中授权人可以通过过滤拍摄照片的时间/日期来查看检测到的图像。

目前,我只能显示目录中的所有文件,没有任何过滤器。非常感谢任何见解或帮助。谢谢! :)
代码:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\Surveillance System\Detected")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo

'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
PictureBox1.Image = Image.FromFile("C:\Surveillance System\Detected\" & ListBox1.Text)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DateTimePicker2.Format = DateTimePickerFormat.Time
DateTimePicker2.ShowUpDown = True
DateTimePicker1.Format = DateTimePickerFormat.Time
DateTimePicker1.ShowUpDown = True
End Sub

button1_click = 显示检测到
button2_click = 清除项目

最佳答案

有两种方法可以做到这一点。一种是按时间作为文件名的一部分列出:

Function GetFiles(ByVal FilterStart As DateTime, ByVal FilterEnd As DateTime) As IEnumerable(Of String)
Dim culture As CultureInfo = CultureInfo.InvariantCulture
Dim FormatString As String = "HHmmss_ddMMMyyyy"
Return Directory.EnumerateFiles("c:\Surveillance System\Detected") _
.Where(Function(f)
Dim Filedate As DateTime = DateTime.ParseExact(f.Replace("pic_", "").Replace(".jpeg", ""), FormatString, culture)
Return Filedate >= FilterStart AndAlso Filedate <= FilterEnd
End Function)
End Function

更新:
我看到你改变了图片。此处提供的格式字符串仅支持原始图片中使用的原始文件名格式。新图片显示了文件名格式的多种约定。如果你真的要为你的文件使用多种名称,你应该考虑使用下面的创建日期选项,或者扩展这个选项以使用 TryParseExact() overload that accepts an array of possible formats .

另一种是使用文件系统中的创建日期信息:
Function GetFiles(ByVal FilterStart As DateTime, ByVal FilterEnd As DateTime) As IEnumerable(Of String)      
Dim di As New DirectoryInfo("c:\Surveillance System\Detected")
Return di.EnumerateFileSystemInfos() _
.Where(Function(f) f.CreationTime >= FilterStart AndAlso f.CreationTime <= FilterEnd) _
.Select(Function(f) f.Name)
End Function

关于vb.net - 如何按日期/时间过滤文件名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21831938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com