每次单击此按钮时如何创建新的txt文件?

时间:2015-05-25 16:48:00

标签: vb.net visual-studio-2010 visual-studio-2012

Private Sub cmdsummary_Click(sender As Object, e As EventArgs) Handles cmdsummary.Click
    Dim writer As New System.IO.StreamWriter("C:\Users\salman\Desktop\Text.txt")
    writer.WriteLine(Me.customer.Text + Me.txtcustomer.Text)
    writer.WriteLine(Me.todaystotal.Text + Me.txtsummarytotal.Text)
    writer.WriteLine(Me.average.Text + Me.txtaveragetotal.Text)
    writer.Close()

End Sub

每次点击cmdsummary它都会编辑记事本文件(txt)并更新它,但我想在每次单击此按钮时创建一个新的txt文件。

1 个答案:

答案 0 :(得分:0)

我认为您每次都必须更改文本文件或文件路径的名称,否则它将继续写入相同的位置(和相同的文件)。

编辑:带简单计数器的代码

Dim filesWritten as Integer = 0

Private Sub cmdsummary_Click(sender As Object, e As EventArgs) Handles cmdsummary.Click
    Dim writer As New System.IO.StreamWriter("C:\Users\salman\Desktop\Text" & filesWritten & ".txt")
    writer.WriteLine(Me.customer.Text + Me.txtcustomer.Text)
    writer.WriteLine(Me.todaystotal.Text + Me.txtsummarytotal.Text)
    writer.WriteLine(Me.average.Text + Me.txtaveragetotal.Text)
    writer.Close()
    filesWritten+=1
End Sub