VB.NET 查找某路径下最新文件夹和最新的TXT文件
任务:得到最新文件夹下的最新TXT中的字符串
Dim MyFullPath As String
Dim MyFullPath1 As String
'排序文件夹,找时间最新的
result_camera = "" '清空结果字符串
MyFullPath = "D:\fluke17bnew\device_2013716\history"
‘查找history问价夹下的最新文件夹
Dimdir As NewIO.DirectoryInfo(MyFullPath)
DimNameList1 As List(OfString) = NewList(Of String)()
DimCreateTimeList1 As List(Of System.DateTime) = New List(Of System.DateTime)()
ForEach d AsIO.DirectoryInfo In dir.GetDirectories
NameList1.Add(d.FullName)
CreateTimeList1.Add(d.CreationTime)
Next
DimfileName1 As String()= NameList1.ToArray()
DimfileCreageTime1 As System.DateTime() =CreateTimeList1.ToArray()
Array.Sort(fileCreageTime1, fileName1)
'MessageBox.Show("目录下最后一个建立的文件名称为:" + fileName1(fileName1.Length - 1) + vbCrLf + "建立时间为:" + fileCreageTime1(fileCreageTime1.Length - 1).ToString())
'MsgBox(fileName1(fileName1.Length- 1))
'排序文本文件,找时间最新的
MyFullPath1 =fileName1(fileName1.Length - 1)
Dimdi As DirectoryInfo = NewDirectoryInfo(MyFullPath1)
Dimlist As List(OfFileInfo) = New List(OfFileInfo)(di.GetFiles())
DimfileNameList As List(OfString) = NewList(Of String)()
DimfileCreateTimeList As List(Of System.DateTime) = NewList(Of System.DateTime)()
ForEach fi AsFileInfo In list
fileNameList.Add(fi.Name)
fileCreateTimeList.Add(fi.CreationTime)
Next
DimfileName As String()= fileNameList.ToArray()
DimfileCreageTime As System.DateTime() =fileCreateTimeList.ToArray()
Array.Sort(fileCreageTime,fileName)
Dimfnn As Integer
Dimaddr As String
fnn = FreeFile()
addr = MyFullPath1 & "\" & fileName(fileName.Length - 1)
'DimOpF As New IO.FileStream(addr, IO.FileMode.Open)
FileOpen(fnn, addr,OpenMode.Input)
Input(fnn, result_camera)
'OpF.Close()
FileClose(fnn)
MsgBox(result_camera)'str就是最后的结果