如何在VB.NET中保存文件?

时间:2015-12-28 22:54:40

标签: vb.net

我试图使用savefiledialog工具,但它不会将文件创建到所选目的地...

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    Dim saveFileDialog1 As New SaveFileDialog()
    saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
    saveFileDialog1.Title = "Save a Text File"
    If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK & saveFileDialog1.FileName.Length > 0 Then
        RichTextBox2.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

VB.NET中的binary logical operator AND用关键字AND表示,而不是使用& (字符串连接运算符)

If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK And 
    saveFileDialog1.FileName.Length > 0 Then
  ....

如果设置Option Strict On此问题将在编译时发出信号 (顺便说一句,没有必要测试文件名长度。如果你不提供文件名,对话框不会关闭)