在vb.net中怎么生成一个txt文件,并且写入一个数组的值?

已举报 回答 关注

在vb.net中怎么生成一个txt文件,并且写入一个数组的值?

  • 回答数

    4

  • 浏览数

    3,457

4个回答 默认排序
  • 默认排序
  • 按时间排序

已采纳
已测试,代码如下:
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
测试截图如下。

取消 评论
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
取消 评论
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fn As String = InputBox("请输入文件名,不需要.TXT后缀", "提示") If fn <> "" Then fn = My.Computer.FileSystem.CombinePath(Application.StartupPath, fn & ".txt") '当前路径和输入文件名组合 My.Computer.FileSystem.WriteAllText(fn, TextBox1.Text, My.Computer.FileSystem.FileExists(fn)) '如果文件已存在,则追加内容,否则建立新文件 End If End Sub
取消 评论
添加一个textbox 一个Button一个SaveFileDialog
程序如下:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SaveFileDialog1.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"
SaveFileDialog1.ShowDialog()
My.Computer.FileSystem.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text, True)
MsgBox("已保存文件到:" & SaveFileDialog1.FileName)
End Sub
End Class
取消 评论
ZOL问答 > 条码打印机 > 博思得条码打印机 > 其他分类 > 在vb.net中怎么生成一个txt文件,并且写入一个数组的值?

举报

感谢您为社区的和谐贡献力量请选择举报类型

举报成功

经过核实后将会做出处理
感谢您为社区和谐做出贡献

提示

确定要取消此次报名,退出该活动?