Results 1 to 12 of 12

Thread: [RESOLVED] stream reader/writer woes

Threaded View

  1. #10
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: stream reader/writer woes

    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:
    1. SaveFileDialog1.InitialDirectory = "C:\test.txt"

    A fileName can simply be a name rather than a full path.
    vb Code:
    1. 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:
    1. Using OFD As New SaveFileDialog
    2.  
    3.          OFD.InitialDirectory = "C:\Temp\"
    4.          OFD.FileName = "myFile"
    5.          OFD.DefaultExt = "text"
    6.          OFD.AddExtension = True
    7.          OFD.Filter = "Text files (*.txt)|*.txt|" & "All files|*.*"
    8.  
    9.          If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
    10.              If OFD.FileName <> String.Empty Then
    11.                  Using sw As New IO.StreamWriter(OFD.FileName, False)
    12.                      For Each str As String In Me.ListBox1.Items
    13.                          sw.WriteLine(str)
    14.                      Next
    15.                      sw.Close()
    16.                  End Using
    17.              End If
    18.          End If
    19. End Using
    Last edited by stimbo; Apr 30th, 2007 at 10:48 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width