'引入命名空间Imports System.IOImports System.Text''' <summary>对文本文件的全部读取</summary> Public Function TxtReader(ByVal Parth As String) As String()Dim StrReader As String = vbNullDim StrArray() As String = NothingTryStrReader = puter.FileSystem.ReadAllText(Parth)StrArray = StrReader.Split(vbCrLf)Catch ex As ExceptionMsgBox("加载文件出错,请检查文件路径或文件名")End TryReturn StrArrayEnd Function''' <summary>对文本文件的全部写入,如果文件不存在,自行创建文件写入</summary> Public Sub TxtWriter(ByVal Parth As String, ByVal StrWriter As String, ByVal TrueOrFalse As Boolean)puter.FileSystem.WriteAllText(Parth, StrWriter, TrueOrFalse)Catch ex As ExceptionMsgBox("加载文件出错,请检查文件路径或文件名")End TryEnd Sub''' <summary>删除文本文件指定行,注意:行号是从0开始记数</summary> Public Sub TxtWriter(ByVal Parth As String, ByVal DeleLine As Integer)Dim StrReader As String = NothingDim StrArray() As String = NothingDim str As String = NothingTry'读取文本StrReader = puter.FileSystem.ReadAllText(Parth)'文本分割给数组,以回车符分割StrArray = StrReader.Split(vbCrLf)'在数组里查找指定下标的数组元素,然后重新创建数组Dim a() As String = StrArray.Where(Function(x) x <> StrArray(DeleLine)).ToArray'将数据重新写入文本If a.Count > 0 ThenFor Each i In astr += i & vbCrLfNextEnd puter.FileSystem.WriteAllText(Parth, str , False)Catch ex As ExceptionMsgBox("加载文件出错,请检查文件路径或文件名")End TryEnd Sub''' <summary>在文本文件指定位置插入行,注意:行号是从0开始记数</summary> Public Sub TxtInsertLine(ByVal Parth As String, ByVal LineNumbers As Integer, ByRef StrInsert As String)Dim StrReader As String = NothingDim StrArray() As String = NothingDim str As String = NothingTry'读取文本StrReader = puter.FileSystem.ReadAllText(Parth)'puter.FileSystem.WriteAllText(Parth, i.ToString & vbCrLf, True)'文本分割给数组,以回车符分割StrArray = StrReader.Split(vbCrLf)'将数组里的元素复制给数组集NewlistDim NewList As List(Of String) = StrArray.Select(Function(x) x.ToString).ToList'在指定位置插入行NewList.Insert(LineNumbers, StrInsert)'将数据重新写入文本If NewList.Count > 0 ThenFor Each i In NewListstr += i.ToString & vbCrLfNextEnd puter.FileSystem.WriteAllText(Parth, str, False)Catch ex As ExceptionMsgBox("加载文件出错,请检查文件路径或文件名")End TryEnd Sub''' <summary>在文本文件末尾添加一行,注意:行号是从0开始记数</summary> Public Sub TxtAddLine(ByVal Parth As String, ByRef StrInsert As String)Dim StrReader As String = NothingDim StrArray() As String = NothingDim str As String = NothingTry'读取文本StrReader = puter.FileSystem.ReadAllText(Parth)'puter.FileSystem.WriteAllText(Parth, i.ToString & vbCrLf, True)'文本分割给数组,以回车符分割StrArray = StrReader.Split(vbCrLf)'将数组里的元素复制给数组集NewlistDim NewList As List(Of String) = StrArray.Select(Function(x) x.ToString).ToList'在数组集尾添加一行数据NewList.Add(StrInsert)'将数据重新写入文本If NewList.Count > 0 ThenFor Each i In NewListstr += i.ToString & vbCrLfNextEnd puter.FileSystem.WriteAllText(Parth, str, False)Catch ex As ExceptionMsgBox("加载文件出错,请检查文件路径或文件名")End TryEnd Sub''' <summary>文件路径,ReaderModel为空时表示全部读取;1,读取一个字符;2读取一行;3,全部读取</summary> Public Function TxtReader(ByVal TxtPath As String, ByVal ReaderModel As Integer) As StringDim reader As StreamReaderDim openF As New OpenFileDialogDim file As FileStreamDim oneC As CharDim oneL As StringDim strL As StringTxtReader = Nothing'openF.ShowDialog() '可以打开一个选择文件的对话框TryopenF.FileName = TxtPath '用了这个就不打开对话框,直接打开file = New FileStream(openF.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)reader = New StreamReader(file, Encoding.Default)If Not reader.EndOfStream ThenIf ReaderModel = "" ThenstrL = reader.ReadToEndTxtReader &= vbCrLf & strLElseSelect Case ReaderModelCase 1oneC = ChrW(reader.Read)TxtReader &= vbCrLf & oneCCase 2oneL = reader.ReadLineTxtReader &= vbCrLf & oneLCase 3strL = reader.ReadToEndTxtReader &= vbCrLf & strLEnd SelectEnd If'TextBox1.Text &= vbCrLf & oneLEnd Ifreader.Close()reader = NothingCatch ex As ExceptionMsgBox("can not open file:" + openF.FileName)Return ""Exit FunctionEnd TryReturn TxtReaderEnd 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 SaveFileDialogDim writer As StreamWriter'Dim file As FileStreamopenF.FileName = txtnamepathTry'file = New FileStream(openF.FileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)If B_boolen = "" Or B_boolen = True Thenwriter = New StreamWriter(openF.FileName, True)writer.WriteLine(str)writer.Flush()writer.Close()Elsewriter = New StreamWriter(openF.FileName, False)writer.WriteLine(str)writer.Flush()writer.Close()End Ifwriter = NothingCatch ex As ExceptionIf txtnamepath = "" Or str = "" ThenMsgBox("文件路径或字符串变量不正确")End IfMsgBox("路径填写可能不正确")End TryEnd Sub
700字范文,内容丰富有趣,生活中的好帮手!