Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've been working on this for a bit and all the examples I've found have been to append a given string into the file, but I haven't been able to find any luck with appending a whole file at the end of a file. Everything's been using .appendAllText or appendText which doesn't suit my needs.

My files are .sgm. In my code I first grab all the sgm files, then I check if that file ends with _Ch#.

I'm ready to append the file to the master, but so far all I've been able to do is append the string text of the file name to the end of the master.

Your help is really appreciated. Max

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(".*_Chd.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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
138 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can read the file content into a string and then use AppendAllText like this:

Imports System.IO
' ...

Dim fileToCopy = Path.Combine(searchDir, result)

'THIS IS WHERE I WANT TO APPEND fileToCopy INTO existingFile
Dim fileContent = File.ReadAllText(fileToCopy)

File.AppendAllText(existingFile, fileContent)

Using Path.Combine is a bit better than concatenating strings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...