vb.net 对文本文件的操作

'引入命名空间
Imports System.IO
Imports System.Text
 ''' <summary>对文本文件的全部读取</summary> 
Public Function TxtReader(ByVal Parth As String) As String()
        Dim StrReader As String = vbNull
        Dim StrArray() As String = Nothing
        Try
            StrReader = My.Computer.FileSystem.ReadAllText(Parth)
            StrArray = StrReader.Split(vbCrLf)
        Catch ex As Exception
            MsgBox("加载文件出错,请检查文件路径或文件名")
        End Try
        Return StrArray
    End Function


''' <summary>对文本文件的全部写入,如果文件不存在,自行创建文件写入</summary> 
Public Sub TxtWriter(ByVal Parth As String, ByVal StrWriter As String, ByVal TrueOrFalse As Boolean)
        Try
            My.Computer.FileSystem.WriteAllText(Parth, StrWriter, TrueOrFalse)
        Catch ex As Exception
            MsgBox("加载文件出错,请检查文件路径或文件名")
        End Try
    End Sub


''' <summary>删除文本文件指定行,注意:行号是从0开始记数</summary> 
 Public Sub TxtWriter(ByVal Parth As String, ByVal DeleLine As Integer)
        Dim StrReader As String = Nothing
        Dim StrArray() As String = Nothing
        Dim str As String = Nothing
        Try
            '读取文本
            StrReader = My.Computer.FileSystem.ReadAllText(Parth)
            '文本分割给数组,以回车符分割
            StrArray = StrReader.Split(vbCrLf)
            '在数组里查找指定下标的数组元素,然后重新创建数组
            Dim a() As String = StrArray.Where(Function(x) x <> StrArray(DeleLine)).ToArray
            '将数据重新写入文本
            If a.Count > 0 Then
                For Each i In a
                    str += i & vbCrLf
                 Next
            End If
            My.Computer.FileSystem.WriteAllText(Parth, str , False)
        Catch ex As Exception
            MsgBox("加载文件出错,请检查文件路径或文件名")
        End Try
    End Sub

''' <summary>在文本文件指定位置插入行,注意:行号是从0开始记数</summary> 
Public Sub TxtInsertLine(ByVal Parth As String, ByVal LineNumbers As Integer, ByRef StrInsert As String)
        Dim StrReader As String = Nothing
        Dim StrArray() As String = Nothing
        Dim str As String = Nothing
        Try
            '读取文本
            StrReader = My.Computer.FileSystem.ReadAllText(Parth)
            'My.Computer.FileSystem.WriteAllText(Parth, i.ToString & vbCrLf, True)
            '文本分割给数组,以回车符分割
            StrArray = StrReader.Split(vbCrLf)
            '将数组里的元素复制给数组集Newlist
            Dim NewList As List(Of String) = StrArray.Select(Function(x) x.ToString).ToList
            '在指定位置插入行
            NewList.Insert(LineNumbers, StrInsert)
            '将数据重新写入文本
            If NewList.Count > 0 Then
                For Each i In NewList
                    str += i.ToString & vbCrLf
                Next
            End If
            My.Computer.FileSystem.WriteAllText(Parth, str, False)

        Catch ex As Exception
            MsgBox("加载文件出错,请检查文件路径或文件名")
        End Try
    End Sub

''' <summary>在文本文件末尾添加一行,注意:行号是从0开始记数</summary> 
Public Sub TxtAddLine(ByVal Parth As String, ByRef StrInsert As String)
        Dim StrReader As String = Nothing
        Dim StrArray() As String = Nothing
        Dim str As String = Nothing
        Try
            '读取文本
            StrReader = My.Computer.FileSystem.ReadAllText(Parth)
            'My.Computer.FileSystem.WriteAllText(Parth, i.ToString & vbCrLf, True)
            '文本分割给数组,以回车符分割
            StrArray = StrReader.Split(vbCrLf)
            '将数组里的元素复制给数组集Newlist
            Dim NewList As List(Of String) = StrArray.Select(Function(x) x.ToString).ToList
            '在数组集尾添加一行数据
            NewList.Add(StrInsert)
            '将数据重新写入文本
            If NewList.Count > 0 Then
                For Each i In NewList
                    str += i.ToString & vbCrLf
                Next
            End If
            My.Computer.FileSystem.WriteAllText(Parth, str, False)
        Catch ex As Exception
            MsgBox("加载文件出错,请检查文件路径或文件名")
        End Try
    End Sub


  ''' <summary>文件路径,ReaderModel为空时表示全部读取;1,读取一个字符;2读取一行;3,全部读取</summary>  
    Public Function TxtReader(ByVal TxtPath As String, ByVal ReaderModel As Integer) As String
        Dim reader As StreamReader
        Dim openF As New OpenFileDialog
        Dim file As FileStream
        Dim oneC As Char
        Dim oneL As String
        Dim strL As String
        TxtReader = Nothing
        'openF.ShowDialog()  '可以打开一个选择文件的对话框
        Try
            openF.FileName = TxtPath           '用了这个就不打开对话框,直接打开
            file = New FileStream(openF.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            reader = New StreamReader(file, Encoding.Default)
            If Not reader.EndOfStream Then
                If ReaderModel = "" Then
                    strL = reader.ReadToEnd
                    TxtReader &= vbCrLf & strL
                Else
                    Select Case ReaderModel
                        Case 1
                            oneC = ChrW(reader.Read)
                            TxtReader &= vbCrLf & oneC
                        Case 2
                            oneL = reader.ReadLine
                            TxtReader &= vbCrLf & oneL
                        Case 3
                            strL = reader.ReadToEnd
                            TxtReader &= vbCrLf & strL
                    End Select
                End If
                'TextBox1.Text &= vbCrLf & oneL
            End If
            reader.Close()
            reader = Nothing
        Catch ex As Exception
            MsgBox("can not open file:" + openF.FileName)
            Return ""
            Exit Function
        End Try
        Return TxtReader
    End Function

 ''' <summary>文件路径加文件名,要写入的字符串,布尔型:为空时为true;true为追加,fale为覆盖</summary>  
    Public Sub TxtWriter(ByVal txtnamepath As String, ByVal str As String, ByRef B_boolen As Boolean)
        Dim openF As New SaveFileDialog
        Dim writer As StreamWriter
        'Dim file As FileStream
        openF.FileName = txtnamepath
        Try
            'file = New FileStream(openF.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
            If B_boolen = "" Or B_boolen = True Then
                writer = New StreamWriter(openF.FileName, True)
                writer.WriteLine(str)
                writer.Flush()
                writer.Close()
            Else
                writer = New StreamWriter(openF.FileName, False)
                writer.WriteLine(str)
                writer.Flush()
                writer.Close()
            End If
            writer = Nothing
        Catch ex As Exception
            If txtnamepath = "" Or str = "" Then
                MsgBox("文件路径或字符串变量不正确")
            End If
            MsgBox("路径填写可能不正确")
        End Try
    End Sub

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值