VB.Net实现文件合并的实例

本文介绍了一个使用VB.NET实现的文件合并程序实例。该程序能够按照文件创建时间排序,将指定目录下的多个文本文件合并为一个输出文件。通过读取每个输入文件的内容,并将其追加到输出文件中,最终完成合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

VB.NET实现文件合并的实例
 

Code:
  1. Private Sub MergeFiles(ByVal inputDir As StringByVal inputMask As StringByVal outputPath As String)   
  2.   
  3.     
  4.     'store files in datatable with their created times to sort by later   
  5.   
  6.     Dim files As New DataTable   
  7.   
  8.     files.Columns.Add("filepath"GetType(String))   
  9.   
  10.     files.Columns.Add("creationtime"GetType(Date))   
  11.   
  12.     'find partial files   
  13.   
  14.     For Each f As String In IO.Directory.GetFiles(inputDir, inputMask)   
  15.   
  16.         files.Rows.Add(New Object() {f, IO.File.GetCreationTime(f)})   
  17.   
  18.     Next  
  19.   
  20.   
  21.   
  22.     'make sure output file does not exist before writing   
  23.   
  24.     If IO.File.Exists(outputPath) Then  
  25.   
  26.         IO.File.Delete(outputPath)   
  27.   
  28.     End If  
  29.   
  30.     
  31.   
  32.     'loop through file in order, and append contents to output file   
  33.   
  34.     For Each dr As DataRow In files.Select("""creationtime")   
  35.   
  36.         Dim contents As String = My.Computer.FileSystem.ReadAllText(CStr(dr("filepath")))   
  37.   
  38.         My.Computer.FileSystem.WriteAllText(outputPath, contents, True)   
  39.   
  40.     Next  
  41.   
  42.     
  43.   
  44. End Sub  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值