VB.NET实现文件合并的实例
- Private Sub MergeFiles(ByVal inputDir As String, ByVal inputMask As String, ByVal outputPath As String)
- 'store files in datatable with their created times to sort by later
- Dim files As New DataTable
- files.Columns.Add("filepath", GetType(String))
- files.Columns.Add("creationtime", GetType(Date))
- 'find partial files
- For Each f As String In IO.Directory.GetFiles(inputDir, inputMask)
- files.Rows.Add(New Object() {f, IO.File.GetCreationTime(f)})
- Next
- 'make sure output file does not exist before writing
- If IO.File.Exists(outputPath) Then
- IO.File.Delete(outputPath)
- End If
- 'loop through file in order, and append contents to output file
- For Each dr As DataRow In files.Select("", "creationtime")
- Dim contents As String = My.Computer.FileSystem.ReadAllText(CStr(dr("filepath")))
- My.Computer.FileSystem.WriteAllText(outputPath, contents, True)
- Next
- End Sub
本文介绍了一个使用VB.NET实现的文件合并程序实例。该程序能够按照文件创建时间排序,将指定目录下的多个文本文件合并为一个输出文件。通过读取每个输入文件的内容,并将其追加到输出文件中,最终完成合并。
1万+

被折叠的 条评论
为什么被折叠?



