writing to a text document

Talk about your favorite PC games, Steam and building awesome custom rigs!

Moderator: Moderators

Skyone
Moderator
Posts: 6390
Joined: Tue Nov 29, 2005 8:35 pm
Location: it is a mystery
Contact:

Post by Skyone »

Hmm, I didn't know you coded, Jeff. :P
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

well, i have this

Code: Select all

  Dim StreamWrite As New StreamWriter("bob.txt")
with the imports, and it creates the document on a button click, which is good.

but when i add this

Code: Select all

streamwrite.writeline(textbox1.text)
nothing goes to the text document
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Yeah,

I used to be a "Report Writer", but my co decided that we would become .net developers instead. Yeah :wink:
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

that would happen if you didn't close / dispose fo the writer after writing.
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

Cool! it worked, but now.. when i try and write again(on the same button click) it says the streamwriter is closed, and i can't write to it (makes sense :lol: ) How do i open it?
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

I'm thinking you might have closed it too early.

or

you'll have to open it again and maybe open with append instead of overwrite....
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

I figured i would have to open it, but there is no

streamwrite.open

lol. Ideas?
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

this is where we opened it.

Code: Select all

Dim sw As New StreamWriter("C:\TestOutput.txt", False) 
I believe that the "False" is overwrite, change it to true for append.

You might want to note the file might have to already exists (not sure) for the initial append...

Jeff
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

Ya, i put that as global, fixed it by putting it in the sub for the click :?

so, now that i can write lines, i need to write it to a new line each time. What i did was this

Code: Select all

Dim intC As Integer = 1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim StreamWrite As New StreamWriter("bob.txt", False)
        Dim sb As New StringBuilder
        Dim str As String
        intC = intC + 1
        str = TextBox1.Text
        StreamWrite.NewLine = intC
        StreamWrite.WriteLine(str)
        StreamWrite.Close()
    End Sub
though all that does is write the most recent line of code with the value of intC at the end of it. Fix?
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

maybe change

Code: Select all

Dim StreamWrite As New StreamWriter("bob.txt", False)
to

Code: Select all

Dim StreamWrite As New StreamWriter("bob.txt", True)
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

getting closer.. that put them all on the same line, and allows me to keep adding more and more, but they stay on that line.
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

Ok, well i just put

Code: Select all

streamreader.newline = vbcrlf
which inserted a line return each click. Now could you teach me how to read lines? :lol:
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Well a streamreader is pretty easy to use also...

Code: Select all

Dim sr As New StreamReader("C:\Anyfile.txt")


        While sr.Read

'Do something

        End While

        sr.Close()
        sr.Dispose()
ghosstt
Senior Member
Posts: 1551
Joined: Mon Feb 26, 2007 4:14 pm

Post by ghosstt »

ya, figured that out on my own. But i need to read just a specific line. is there a way to read say, only line 5?
jeffslot
Portablizer Extraordinaire
Posts: 853
Joined: Wed Jun 01, 2005 6:26 am
Location: PA
Contact:

Post by jeffslot »

Ok, here is my last bit for tonight.

I think that you might be able to use a for next loop to read whatever line you want.

For instance if you are only interested in reading line number five, you'd only read the line if your counter is equal to the line number you want to read.

Good luck with your project & keep us posted!

Jeff
Post Reply