【发布时间】:2019-08-15 19:40:51
【问题描述】:
我一直在研究这个问题,我发现的所有示例都是将给定的字符串附加到文件中,但是我无法找到任何运气将整个文件附加到文件的结尾。一切都在使用不适合我需要的 .appendAllText 或 appendText。
我的文件是.sgm。在我的代码中,我首先获取所有 sgm 文件,然后检查该文件是否以 _Ch# 结尾。
我已准备好将文件附加到主文件,但到目前为止,我所能做的只是将文件名的字符串文本附加到主文件的末尾。
非常感谢您的帮助。 最大
Public Class Form1
Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
Dim searchDir As String = txtSGMFile.Text 'input field for user
'Get all the sgm files in the directory specified
Dim fndFiles = Directory.GetFiles(searchDir, "*.sgm")
'Set up the regular expression you will make as the condition for the file
Dim rx = New Regex(".*_Ch\d\.sgm")
Dim ch1 = New Regex(".*_Ch[1]\.sgm")
Dim existingFile = searchDir & "\Bld1_Master_Document.sgm"
'Loop through each file found by the REGEX
For Each file In fndFiles
If rx.IsMatch(file) Then
If ch1.IsMatch(file) Then
Dim result = Path.GetFileName(file)
Dim fileToCopy = searchDir & "\" & result
'THIS IS WHERE I WANT TO APPEND fileToCopy INTO existingFile
System.IO.File.AppendAllText(fileToCopy, existingFile)
MessageBox.Show("File Copied")
End If
'MsgBox(file)
End If
Next file
Close()
End Sub
【问题讨论】:
-
.sgm 是否意味着它们包含 SGML?
-
是 SGM 表示 SGML。但是就像文本文件一样,只要它们满足要求,就可以附加到一个和另一个中
-
在我看来,
Dim result = Path.GetFileName(file)Dim fileToCopy = searchDir & "\" & result与Dim fileToCopy = file相比还有很长的路要走——或者问题中省略了其他代码? (Directory.GetFiles 返回完整路径以及文件名。) -
我把完整路径放进去是因为代码会在我们系统的多个子文件夹中运行
标签: vb.net appendfile