How to Find and Replace text file

โปรแกรมค้นหาคำ และแก้ไขคำ จะคล้ายกับโปรแกรม Find and Replace ทั่วๆไป




Imports System.IO

Public Class Form1
    Dim sContents As String
    Dim sErr As String = ""
    Dim bAns As Boolean
    Public Function GetFileContents(ByVal FullPath As String, _
       Optional ByRef ErrInfo As String = "") As String


        Dim strContents As String
        Dim objReader As StreamReader
        Try


            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            objReader.Close()
            Return strContents
        Catch Ex As Exception
            ErrInfo = Ex.Message
        End Try
    End Function


    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean


        Dim Contents As String
        Dim bAns As Boolean = False
        Dim objReader As StreamWriter
        Try
            objReader = New StreamWriter(FullPath)
            objReader.Write(strData)
            objReader.Close()
            bAns = True
        Catch Ex As Exception
            ErrInfo = Ex.Message


        End Try
        Return bAns
    End Function


    Private Sub btnReadFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadFile.Click
        sContents = GetFileContents(txtPathFile.Text, sErr)
        txtReadFile.Text = sContents
    End Sub


    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim fdlg As New OpenFileDialog
        fdlg.Title = "Open File Dialog"
        fdlg.InitialDirectory = "c:\"
        fdlg.Filter = "Text files (*.txt)|*.txt|XML files (*.xml)|*.xml|All files (*.*)|*.*"
        fdlg.FilterIndex = 1
        fdlg.RestoreDirectory = True
        If fdlg.ShowDialog() = DialogResult.OK Then
            txtPathFile.Text = fdlg.FileName
        End If
    End Sub


    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Close()
    End Sub


    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If sErr = "" Then
            Debug.WriteLine("File Contents: " & sContents)
            'Save to different file
            Dim FileName As String


            bAns = SaveTextToFile(sContents, txtPathFile.Text, sErr)
            If bAns Then
                Debug.WriteLine("File Saved!")
            Else
                Debug.WriteLine("Error Saving File: " & sErr)
            End If


        Else
            Debug.WriteLine("Error retrieving file: " & sErr)
        End If
    End Sub


    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        Dim str As String


        'เพิ่มเข้าไปทางด้านหน้าของคำค้นรอง
        str = Replace(sContents, txtSearch.Text, txtAddText.Text)
        'End While
        txtChange.Text = str
    End Sub
End Class

ไม่มีความคิดเห็น:

แสดงความคิดเห็น