已測(cè)試,代碼如下:
成都創(chuàng)新互聯(lián)公司自2013年創(chuàng)立以來(lái),先為湘陰等服務(wù)建站,湘陰等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為湘陰企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
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
'取得存儲(chǔ)的文本
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
Next
Return?str
End?Function
'存儲(chǔ)文本
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
End?Sub
'取得存儲(chǔ)路徑
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?Function
End?Class
測(cè)試截圖如下。
Dim SaveFileDialog1 As New SaveFileDialog()
'創(chuàng)建一個(gè)保存對(duì)話框
SaveFileDialog1.Filter = "txt files (*.txt)|*.txt"
'設(shè)置
擴(kuò)展名
If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
'如果確定保存
My.Computer.
FileSystem
.WriteAllText(SaveFileDialog1.Filename, Textbox1.Text,False)
'保存文本,False表示不追加文本,直接覆蓋其內(nèi)容
End If
設(shè)計(jì)一個(gè)窗口,添加一個(gè)名為textBox1的System.Windows.Forms.TextBox,
添加一個(gè)名為button1的System.Windows.Forms.Button。
為button1的單擊事件添加如下處理函數(shù):
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 If
End Try
End Sub