我的winform上有一个标签,当表单加载时,计算目录中的文件数量。我想在winform应用程序运行时每nTH秒更新一次该标签。我不确定如何实现这一点,我负责计算文件:
Public Function getUserCountsTotal() As Integer
Dim di As New DirectoryInfo("C:\myDirectory")
Dim Users As FileInfo() = di.GetFiles().OrderByDescending(Function(fi) fi.LastWriteTime).ToArray()
Dim user As FileInfo
'list the names of all files in the specified directory
For Each user In Users
ComboBox1.Items.Add(Path.GetFileNameWithoutExtension(user.Name) & "- " & user.LastWriteTime)
Next
getUserCountsTotal = ComboBox1.Items.Count + 1
End Function
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
statusPanel.Text = ""
statusPanel.Text = "Logged in as " & getYourUserName() & " - " & "[ " & getUserCountsTotal() & " ] "
End Sub
答案 0 :(得分:0)
在表单上放置一个计时器,并将Enabled
属性设置为 true 。双击计时器并输入计算目录中文件的代码:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim counter = My.Computer.FileSystem.GetFiles("C:\myDirectory")
Label1.Text = counter.Count
End Sub
您可以在Timer的Interval
属性中设置间隔(以毫秒为单位)。要每两秒更新一次,请将值设置为2000,每三秒更新一次,将其设置为3000等。