十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
创新互联主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、成都营销网站建设、网站程序开发、HTML5响应式成都网站建设、手机网站开发、微商城、网站托管及网站维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为玻璃钢雕塑行业客户提供了网站设计服务。
FileOpen(1, "c:\34.txt", OpenMode.Output, OpenAccess.Write)
PrintLine(1, TextBox1.Text)
FileClose(1)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
FileOpen(1, "c:\34.txt", OpenMode.Input, OpenAccess.Read)
Dim text As String = ""
Do Until EOF(1)
text += LineInput(1) vbCrLf
Loop
TextBox2.Text = text
FileClose(1)
End Sub
还是有点不同,vb6的print等同于这里printLine, print 类似于vb6的write,而且不换行。
至于你错在哪里我不知道了,反正我的代码测试了一下没问题。
窗体上有两个多行文本框,两个按钮(写入和读取)
补充:我想出一个原因,可能你没有关闭文件吧。写入的部分内容还在内存缓冲区,还没完全写入磁盘。
我原来写的一个方法
Private Sub StrToFile(ByVal cExpression As String, ByVal cFileName As String, ByVal lAdditive As Boolean)
If File.Exists(cFileName) And Not lAdditive Then
File.Delete(cFileName)
End If
Dim oFs As FileStream = New FileStream(cFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim oWriter As StreamWriter = New StreamWriter(oFs)
Dim C2_STR_FILE As Boolean = True
oWriter.BaseStream.Seek(0, SeekOrigin.End)
oWriter.Write(vbCrLf)
oWriter.Write(cExpression)
oWriter.Flush()
oWriter.Close()
oFs.Close()
End Sub
当然能了
你可以将你的内容按行分别存储在数组变量中
a="全部内容"
b=split(a,vbcrlf)
b(0) 即第一行 保存文件1
b(1) 即第二行 保存文件2
b(2)-b(x) 就是剩下内容
Dim i As Integer
Dim j As Integer
Dim myFile As New System.IO.StreamWriter("c:\Test.txt")
Dim strTemp As String = ""
For j = 0 To frmQuery.DataGridView1.RowCount - 1
strTemp = ""
For i = 0 To frmQuery.DataGridView1.Columns.Count - 1
strTemp = "'" frmQuery.DataGridView1(i, j).Value "'" ","
Next
myFile.WriteLine(strTemp)
Next
myFile.Close()
格式为表格每行保存为文本文件的一行,字段之间以逗号分隔。
直接给你保存和读取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
窗体上建立BUTTON1和TEXTBOX1
代码如下
100%正确,刚刚写的,调试好了
Imports System.io
Public Class form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As New FileStream("d:\文本.txt", FileMode.Create, FileAccess.Write)
Dim r As New StreamWriter(fs)
Dim I As Integer
r.Write(TextBox1.Lines(I))
r.Flush()
r.Close()
End Sub
End Class
路径自己修改
(你的给分是我回答的动力)