Daniele Perrella
Daniele Perrella

Reputation: 41

how to save a file in VB.NET?

i'm trying to use the savefiledialog tool, but it don't create the file to the selected destination...

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

Upvotes: 1

Views: 181

Answers (1)

Steve
Steve

Reputation: 216243

The binary logical operator AND in VB.NET is expressed with the keyword AND not using & (string concatenating operator)

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

If you set Option Strict On this problem will be signaled at compile time (By the way, there is no need to test for the filename length. The dialog doesn't close if you don't supply a filename)

Upvotes: 1

Related Questions