已测试,代码如下:
创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都做网站、宜黄网络推广、小程序设计、宜黄网络营销、宜黄企业策划、宜黄品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供宜黄建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
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
Next
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
End Sub
'取得存储路径
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
测试截图如下。
是的。vb.net不能在c盘建立txt文件是由于其格式不支持所导致的,通过更换文件格式即可解决。c盘通常下是系统盘,也就是主要存放安装操作系统和软件的分区盘。
设计一个窗口,添加一个名为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 If
End Try
End Sub
Dim SaveFileDialog1 As New SaveFileDialog()
'创建一个保存对话框
SaveFileDialog1.Filter = "txt files (*.txt)|*.txt"
'设置
扩展名
If SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
'如果确定保存
My.Computer.
FileSystem
.WriteAllText(SaveFileDialog1.Filename, Textbox1.Text,False)
'保存文本,False表示不追加文本,直接覆盖其内容
End If