vb.net 如何遍历目录列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2216101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net how to loop through a directory listing?
提问by Alex
How can I loop through a folder getting each file listed and when its date/time?
如何遍历文件夹以获取列出的每个文件及其日期/时间?
回答by micahtan
Use DirectoryInfo.GetFiles() and extract the data (Name, CreationTime, etc.) from the FileInfo class.
使用 DirectoryInfo.GetFiles() 并从 FileInfo 类中提取数据(名称、创建时间等)。
I've pasted some code from the MSDN page here.
我在此处粘贴了 MSDN 页面中的一些代码。
Imports System
Imports System.IO
Public Class GetFilesTest
Public Shared Sub Main()
' Make a reference to a directory.
Dim di As New DirectoryInfo("c:\")
' Get a reference to each file in that directory.
Dim fiArr As FileInfo() = di.GetFiles()
' Display the names of the files.
Dim fri As FileInfo
For Each fri In fiArr
Console.WriteLine(fri.Name)
Next fri
End Sub 'Main
End Class 'GetFilesTest
回答by ?zgür Hocao?lu
For Each LogFile In Directory.GetFiles(Application.StartupPath & "\Txt\")
' do whatever wtih filename
Next
回答by schlebe
we have the chance to develop in VB.Net (not in Java) and some variable's definitions can be shortened.
我们有机会在VB.Net(而不是Java)中进行开发,并且可以缩短一些变量的定义。
I still use GetFiles() and I have added the code to display the DateTime infos.
我仍然使用 GetFiles() 并且我添加了代码来显示日期时间信息。
Imports System
Imports System.IO
...
Dim dir As New DirectoryInfo(sFolderName)
For Each f In dir.GetFiles()
Console.WriteLine(">> FILE-NAME: [" & f.Name & "]")
Console.WriteLine(">> UPDATE-DATE: " & f.lastWriteTime.toString("YYYY-MM-DD"))
Console.WriteLine(">> CREATE-DATE: " & f.creationTime.toString("YYYY-MM-DD"))
Next