求VB.NET写的自动保存txt文档的代码
发布网友
发布时间:2023-07-04 22:28
我来回答
共2个回答
热心网友
时间:2024-03-16 12:21
直接给你保存和读取TXT的VB.NET的函数代码,你只要在触发事件中调用就可以了,注意换行,有的是显示不下去才显示两行的,还有strFilePath代表文件路径,TempENG代表文件编码格式如:UTF-8或者GB2312,strText 代表内容字符串。
#Region "读取TXT"
Public Function ReadTxt(ByVal strFilePath As String, ByVal TempENG As String) As String
Dim mySr As System.IO.StreamReader
Dim strS As String
Dim n%
strS = ""
mySr = New System.IO.StreamReader(strFilePath, System.Text.Encoding.GetEncoding(TempENG))
Do
Dim line$ = mySr.ReadLine()
strS = strS & line & vbCrLf
n = n + 1
Loop Until mySr.EndOfStream = True
mySr.Close()
Return strS
End Function
#End Region
#Region "保存TXT"
Public Function SaveTxt(ByVal strFilePath As String, ByVal strText As String, ByVal TempENG As String) As Boolean
Try
If Dir(strFilePath) <> "" Then File.Delete(strFilePath)
Dim mySw As System.IO.StreamWriter
Dim strLine() As String
mySw = New System.IO.StreamWriter(strFilePath, True, System.Text.Encoding.GetEncoding(TempENG))
strLine = Split(strText, vbCrLf)
For i As Integer = 0 To UBound(strLine)
mySw.WriteLine(strLine(i))
Next
mySw.Close()
Return True
Catch ex As Exception
Return False
End Try
End Function
#End Region
热心网友
时间:2024-03-16 12:22
楼上正解