如何在Visual Basic 2008+中找到驱动器中的文件数? [英] How do I find the number of files in a drive in Visual Basic 2008+ ?
本文介绍了如何在Visual Basic 2008+中找到驱动器中的文件数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Visual Basic中检测硬盘驱动器中的临时文件数量?我正在使用此代码,但它不起作用:
How can I detect the number of temporary files in the hard drive in Visual Basic? I''m using this code, and it''s not working:
Dim drive As String = Path.GetPathRoot(System.Environment.ExpandEnvironmentVariables("%SystemDrive%"))
Dim counter3 As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
counter3 = My.Computer.FileSystem.GetFiles(drive, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
label1.text = (CStr(counter3.Count))
推荐答案
Module Module1
Dim cnt As Integer
Sub Main()
cnt = 0
Console.WriteLine(CountFiles())
Console.Read()
End Sub
Function CountFiles(Optional ByVal dri As String = "C:\", Optional ByVal subDir As Boolean = True) As Integer
On Error Resume Next
For Each fo As String In System.IO.Directory.GetDirectories(dri)
If subDir Then
CountFiles(fo, True)
End If
Next
For Each fi As String In System.IO.Directory.GetFiles(dri)
cnt += 1
Next
Return cnt
End Function
End Module
好,我找到了您的解决方案,您的驱动器字符串包含"C:",您需要在其后附加"\".
Ok, I found your solution, your drive string contains ''C:'' you need to append the ''\'' to it.
Dim drive As String = Path.GetPathRoot(System.Environment.ExpandEnvironmentVariables("%SystemDrive%")) & "\"
Dim counter3 As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
counter3 = My.Computer.FileSystem.GetFiles(drive, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
Label1.Text = (CStr(counter3.Count))
当我测试您的代码时,我发现它正在搜索我的可执行文件所在的目录;不是系统根目录.
如果更改此行,我将获得有效结果...
counter3 = My.Computer.FileSystem.GetFiles(驱动器,FileIO.SearchOption.SearchAllSubDirectories,"* .tmp")
为此...
counter3 = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp,FileIO.SearchOption.SearchAllSubDirectories,"* .tmp")
When I test your code I find it searching the directory my executable is in; not the system root.
I get valid results if I change this line...
counter3 = My.Computer.FileSystem.GetFiles(drive, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
To this...
counter3 = My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp, FileIO.SearchOption.SearchAllSubDirectories, "*.tmp")
这篇关于如何在Visual Basic 2008+中找到驱动器中的文件数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文