Public Class Form1
Private Sub 写入文件_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 写入文件.Click
FileOpen(1, "D:\一行文件.txt", OpenMode.Output) ' Open file for output.
WriteLine(1, "想只保留文件为一行 那你就写一行好了") ' Print blank line to file.
FileClose(1) ' Close file.
End Sub
Private Sub 读取文件_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 读取文件.Click
Dim TextLine As String
FileOpen(1, "D:\一行文件.txt", OpenMode.Input) ' Open file for output.
TextLine = LineInput(1) '读取一行就了
MsgBox(TextLine)
FileClose(1)
End Class
最简单的方法 还有很多方法 自己研究吧亲 希望能帮到你
设计一个窗口,添加一个名为textBox1的System.Windows.Forms.TextBox,
添加一个名为button1的System.Windows.Forms.Button.
为button1的单击事件添加如下处理函数:
Sub Button1Click(sender As Object, e As EventArgs)
'f是你的文本文件的文件名
Const f As String="t.txt"
Dim sw As System.IO.StreamWriter=Nothing
Try
If Not System.IO.File.Exists(f) Then
sw=System.IO.File.CreateText(f)
Else
sw=New System.IO.StreamWriter(f,True)
End If
sw.WriteLine(textBox1.Text)
Finally
If sw IsNot Nothing Then
sw.close()
End Try
function writetxt (filename as string,currenttxt as string) filename ="c:\" filename ".txt" 'filename要生成的文件名 currenttxt当前写入的文件内容. open filename for output As #1 print #1, currenttxt close #1 end functiong sub xxx ... '定义rs为recorder,并生成记录集, ... rs.movefirst while not rs.eof call writetxt rs.filds("文章标题").value ,rs.filds("文章").value rs.movenext loop '关闭记录集等 end sub
已测试,代码如下:
Imports?System.IO?'引入文本操作
Public?Class?Form1
Private?Sub?Button1_Click(ByVal?sender?As?System.Object,?ByVal?e?As?System.EventArgs)?Handles?Button1.Click
Dim?txt?As?String?=?Me.getTxt
SaveTxt(txt)
End?Sub
'取得存储的文本
Private?Function?getTxt()?As?String
Dim?x(0?To?10)?As?Integer
For?i?As?Integer?=?0?To?x.Length?-?1
x(i)?=?CInt(100?*?Rnd())
Next
Dim?str?As?String?=?""
For?j?As?Integer?=?0?To?x.Length?-?1
str?◆=?x(j).ToString?vbCrLf
Return?str
End?Function
'存储文本
Private?Sub?SaveTxt(ByVal?v?As?String)
Dim?fp?As?String?=?GetSavePath()
If?String.IsNullOrEmpty(fp)?=?False?Then
'StreamWriter
Dim?Swriter?As?New?StreamWriter(fp,?False)?'覆盖或新建
Swriter.WriteLine(v)
Swriter.Close()
End?If
'取得存储路径
Private?WithEvents?sd?As?New?SaveFileDialog
Private?Function?GetSavePath()?As?String
sd.FileName?=?""
sd.AddExtension?=?True
sd.DefaultExt?=?"txt"
sd.Filter?=?"TXT文件(*.txt)|*.txt"
sd.ShowDialog()
Return?sd.FileName
End?Class
测试截图如下.
代码如下:
'写文本文件
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'创建(写入)一个文本文件
Dim MyStream As New System.IO.FileStream(Application.StartupPath "\Ssk.txt", System.IO.FileMode.Create)
Dim MyWriter As New System.IO.StreamWriter(MyStream, System.Text.Encoding.Default)
MyWriter.WriteLine(TextBox1.Text)
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
'读文本文件
'读取一个文本文件
MyReader.Close()
气斜射入水或其他介质,折射光线与入射光线法线在
以上就是土嘎嘎小编为大家整理的关于vb.net写txt文件的信息相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!