It creates one automatically for you if it doesn't already exist. There are a few problems with your code:
This isn't a directory is it...
vb Code:
SaveFileDialog1.InitialDirectory = "C:\test.txt"
A fileName can simply be a name rather than a full path.
vb Code:
SaveFileDialog1.FileName = filename
The main problem is your logic. You create a StreamWriter with a path before they have chosen where to Save it. That's what's really causing problems. Declare and use the Streamwriter once the checks for the OK button have been done and the fileName field isn't empty (see below).
For the streamWriter try implementing Using/End Using as suggested above for the StreamWriter. What's the point of asking for help, getting that help and then promptly ignoring it??
vb Code:
Using OFD As New SaveFileDialog OFD.InitialDirectory = "C:\Temp\" OFD.FileName = "myFile" OFD.DefaultExt = "text" OFD.AddExtension = True OFD.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*" If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then If OFD.FileName <> String.Empty Then Using sw As New IO.StreamWriter(OFD.FileName, False) For Each str As String In Me.ListBox1.Items sw.WriteLine(str) Next sw.Close() End Using End If End If End Using




Reply With Quote