none
working with streamwriter

    Question

  • Hi,

    working with Streamwtiter

    in vb6 I use the following expression to transfer table information to a text file:
     Open ("c:\cob2cef.txt" )For Output As #1

          Do While Not rsclient.EOF
          Write #1, rsEmpresa!Cod_Sacado & ......

               rsclient.MoveNext()
          Loop
        Close #1
    the output keyword cleans the file.
    in vb.net i use the :
     Using writer As StreamWriter = New StreamWriter("G:\append.txt", True)
                writer.WriteLine("xxxxxxxx")
            End Using

    how can I "kill" the file and creat it again befor the use of the streamwriter
    Thanks
    Dov



    Friday, January 10, 2014 9:09 PM

Answers

  • If you mean delete the file, then you could set append parameter to false as follows:

            Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("G:\append.txt", False)
                writer.WriteLine("xxxxxxxx")
            End Using

    or you could delete the file as follows:

    System.IO.File.Delete("G:\append.txt")
            Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("G:\append.txt", True)
                writer.WriteLine("xxxxxxxx")
            End Using

    • Marked as answer by Dovk179 Monday, January 13, 2014 2:58 PM
    Saturday, January 11, 2014 1:03 AM

All replies

  • If you mean delete the file, then you could set append parameter to false as follows:

            Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("G:\append.txt", False)
                writer.WriteLine("xxxxxxxx")
            End Using

    or you could delete the file as follows:

    System.IO.File.Delete("G:\append.txt")
            Using writer As System.IO.StreamWriter = New System.IO.StreamWriter("G:\append.txt", True)
                writer.WriteLine("xxxxxxxx")
            End Using

    • Marked as answer by Dovk179 Monday, January 13, 2014 2:58 PM
    Saturday, January 11, 2014 1:03 AM
  • Hi,

    I have moved this thread to Visual Basic forum for better response.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, January 13, 2014 2:41 AM
  • You would only use that particular constructor if you specifically did want to append to the existing file or you might want to append and you had a Boolean variable that indicated that.  If you know for a fact that you don't want to append then why use that constructor at all?  Just use the constructor that takes only a file name and it will then overwrite the file with any new text that you write.

    This is a prime example of why people should read the MSDN documentation.  It's there to help, which explains why it's accessible from the Help menu in VS.  If you'd simply read the documentation for the StreamWriter and its constructors then you'd know what each one does and why you're using that one the wrong way and why it's not the best one to use in this situation anyway.

    Monday, January 13, 2014 5:53 AM
  • jmcilhinney, you are correct. But, it really does not make one difference here. Internally, the StreamWriter(System.String) and StreamWriter(System.String,False) make the same exact call to it's base constructor. The point of the code was to show Dovk179 that setting the parameter to False would overwrite the file.

    Monday, January 13, 2014 7:02 AM
  • Unlike as VB6 has newer VB versions much more posibilities to write a file, even that nasy old one.

    The most easy ones to use are the IO.File class, where probably the write all lines and write all text fits the best for your current problem.

    http://msdn.microsoft.com/en-us/library/system.io.file_methods(v=vs.110).aspx

    The streamwriter you would use for instance on the Net or for special files like images and movies.



    Success
    Cor

    Monday, January 13, 2014 12:39 PM