简体   繁体   English

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

[英]How do I create a new txt file every time I click on this button?

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

Every time I click on cmdsummary it edits the notepad file (txt) and updates it but I want to create a new txt file everytime I click on this button. 每次我单击cmdsummary时,它都会编辑记事本文件(txt)并更新它,但是我想每次单击此按钮时都创建一个新的txt文件。

I think you'd have to change the name of the textfile or filepath each time, or else it will just keep writing to the same place (and same file). 我认为您每次都必须更改文本文件或文件路径的名称,否则它将一直写入相同的位置(和相同的文件)。

EDIT: Code with simple counter 编辑:带有简单计数器的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM