Streamwriter Problem

Jay1b

Well-known member
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
I copied the majority of the following text from the msdn library,
but I have those annoying lilttle blue lines under the imports section and the file in the file.append bit.

Imports System
Imports System.IO

Public Sub ReadBuffer2()

Dim path As String = "c:\MyTest.txt"
Dim sw As StreamWriter

sw = File.AppendText(path)
sw.WriteLine("This")
sw.WriteLine("is Extra")
sw.WriteLine("Text")
sw.Flush()
sw.Close()

End Sub

I can get arid of the imports section by putting system.io before streamwriter - but i still get the line under the FILE in file.append

Thanks.
 
I would just like to say that i have done this another way, but i just want to know why this isnt working. As i cant see why it shouldnt.

The way i have done it is:

Public Sub ReadBuffer2()

Dim sw As New System.IO.StreamWriter("C:\test.txt", True)

sw.WriteLine("This")
sw.WriteLine("is Extra")
sw.WriteLine("Text")
sw.Flush()
sw.Close()

End Sub

But could someone please tell me why the first script didnt work please?
 
the easiest way would be like this....
Code:
        Dim path As String = "c:\MyTest.txt"

        Dim sWriter As New StreamWriter(New FileStream(path, FileMode.OpenOrCreate))
        With sWriter
            .WriteLine("test")
            .WriteLine("123")
            .WriteLine("456")
        End With
        sWriter.Close()

although when i tried your first method....
Code:
        Dim path As String = "c:\MyTest.txt"
        Dim sw As StreamWriter

        sw = File.AppendText(path)
        sw.WriteLine("This")
        sw.WriteLine("is Extra")
        sw.WriteLine("Text")
        sw.Flush()
        sw.Close()
that also worked and that could also be shortned slightly to still work , like this...
Code:
        Dim path As String = "c:\MyTest.txt"
        Dim sw As StreamWriter = File.AppendText(path)
        With sw
            .WriteLine("This")
            .WriteLine("is Extra")
            .WriteLine("Text")
            .Flush()
        End With
        sw.Close()
 
hmmm......

When i tried it, it found a problem with the FILE bit, namely File itself. Maybe the bit infront of the FILE hadnt been imported correctly. I know streamwriter is system.io, what would FILEs be?

Thank you.
 
I was just getting the blue line underneath it, i have found another way of doing it, i just wanted to know why it didnt work.

It was because the system.io was being called infront of the FILE.

When you use:
IMPORTS System
IMPORTS System.IO

Can these go anywhere, or just in certain places?
 
The imports need to go above the class declaration - I just put them at the top of the file below Option Explicit On and Option Strict On
 
Back
Top