.net – saving a string as a csv file

vb.net

in a winform how do i get one of those fancy windows "save file as" dialogues to appear so that the user can save a string as a file to a user-specified location on their hardrive?

Best Solution

Dim myString = "Hello world!"  
Dim saveFileDialog As New SaveFileDialog()
saveFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog.FilterIndex = 2
saveFileDialog.RestoreDirectory = True

If saveFileDialog.ShowDialog() = DialogResult.OK Then
  If saveFileDialog.FileName <> "" Then
        System.IO.File.WriteAllText(saveFileDialog.FileName, myString)
  End If
End If

There's not a whole lot to it. Specify the file types to save as (in a fairly arcane format; review the docs for the Filter property to learn more), then show the dialog, and either a) retrieve the stream to write to (as shown here) with the OpenFile method, or retrieve the filename selected with the FileName property.

Review the docs on MSDN for more.

Related Question