在vb.net中如何生成一个txt文件,并且写入一个数组的值?
我想生成一个记事本文件,这个文件中显示:
1
2
......
i 展开
已测试,代码如下:
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
测试截图如下。