如何将Textboxt.Text保存为文件名?

时间:2018-03-19 03:05:58

标签: vb.net

这段代码出了什么问题?

我需要添加textbox13.text(它的值在数字中)作为文件名,如何添加一个选项来检查文件是否已经存在,如果这样显示一个选项说替换或取消当我按我的保存按钮。

到目前为止,这是代码:

Dim i As Integer = 0
Dim filepath As String = IO.Path.Combine("D:\Logs", Textbox13.Text + i.ToString() + ".txt")        
Using sw As New StreamWriter(filepath)
sw.WriteLine(TextBox13.Text)
sw.WriteLine(TextBox1.Text)
sw.WriteLine(TextBox2.Text)
sw.WriteLine(TextBox3.Text)
sw.WriteLine(TextBox4.Text)
sw.WriteLine(TextBox5.Text)

sw.WriteLine(TextBox7.Text)

sw.WriteLine(TextBox9.Text)
sw.WriteLine(TextBox10.Text)
sw.WriteLine(TextBox11.Text)
sw.WriteLine(TextBox12.Text)

这是来自Form1

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Form2.WriteTextBoxTextToLabel(TextBox1.Text)
    Form2.WriteTextBoxTextToTextbox(TextBox1.Text)
End Sub

这是来自form2

Public Sub WriteTextBoxTextToLabel(ByVal Txt As String)
    lblPD.Text = Txt
End Sub
Public Sub WriteTextBoxTextToTextbox(ByVal Txt As String)
    TextBox13.Text = Txt
End Sub

Private Sub TextBox13_TextChanged(sender As Object, e As EventArgs) Handles TextBox13.TextChanged
    TextBox13.Text = lblPD.Text

End Sub

1 个答案:

答案 0 :(得分:0)

您可以在创建StreamWriter之前添加以下逻辑:

if system.io.file.exists(filepath) then
   ' The file exists
else
   ' The file does not exist
end if