Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,538 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,769 people online right now. Registration is fast and FREE... Join Now!




How to Write to Certain Line?

 
Reply to this topicStart new topic

How to Write to Certain Line?

animedude123
20 Aug, 2008 - 11:01 AM
Post #1

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 45


My Contributions
Hey,

I'm using a program to take the user's input and write it to a certain line in a text file. There are 5 different text boxes taking user input, and each one needs to be written to another line. Right now though all I an get is the user's input to write to the first line.

So how do I get it so each input is written to a different line? This is a snip of my current code:

CODE
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If File.Exists(Application.StartupPath & "\world.properties") Then 'Check if the file exists before doing things
            Dim theStreamWriter As New StreamWriter(Application.StartupPath & "\world.properties")
            theStreamWriter.WriteLine(Me.TextBox1.Text)
            theStreamWriter.Close()
        Else
            MsgBox("World.properties is not found")
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If File.Exists(Application.StartupPath & "\world.properties") Then 'Check if the file exists before doing things
            Dim theStreamWriter As New StreamWriter(Application.StartupPath & "\world.properties")
            theStreamWriter.WriteLine(Me.TextBox2.Text)
            theStreamWriter.Close()
        Else
            MsgBox("World.properties is not found")
        End If
    End Sub


I need the first one to write to line 2, and the second one to write to line 4. Any help would be great biggrin.gif

Thanks in advance for any help!
User is offlineProfile CardPM
+Quote Post

djkitt
RE: How To Write To Certain Line?
20 Aug, 2008 - 12:57 PM
Post #2

D.I.C Head
**

Joined: 22 May, 2008
Posts: 128



Thanked: 13 times
My Contributions
So, I think the easiest way to do what you want is to just rewrite the entire file each time any one textbox is changed.

Hope this helps,

Kitt


User is offlineProfile CardPM
+Quote Post

animedude123
RE: How To Write To Certain Line?
20 Aug, 2008 - 05:16 PM
Post #3

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 45


My Contributions
No, that wouldn't work. The details on why is kinda a hassle, so I won't go into it, and just leave it with that not being an option.

Is writing to a certain line really that hard? blink.gif
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: How To Write To Certain Line?
20 Aug, 2008 - 06:08 PM
Post #4

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 799



Thanked: 51 times
Dream Kudos: 2175
My Contributions
QUOTE(animedude123 @ 21 Aug, 2008 - 02:16 AM) *

No, that wouldn't work. The details on why is kinda a hassle, so I won't go into it, and just leave it with that not being an option.

Is writing to a certain line really that hard? blink.gif

Suggested Reading Tutorial
Tutorial
User is offlineProfile CardPM
+Quote Post

animedude123
RE: How To Write To Certain Line?
20 Aug, 2008 - 08:19 PM
Post #5

New D.I.C Head
*

Joined: 3 Aug, 2007
Posts: 45


My Contributions
Thanks for the link. It has what I want and now I'm using this:

CODE
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If File.Exists(Application.StartupPath & "\world.properties") Then 'Check if the file exists before doing things
            Using reader As New StreamReader("world.properties")
                'get the contents of the file
                Dim contents As String = reader.ReadToEnd()
                'insert the specified line
                contents.Insert(2, Me.TextBox1.Text)
                'now we need to rewrite the text in the file
                Using writer As New StreamWriter("world.properties", False)
                    'write the file with the new line
                    writer.Write(contents)
                End Using
            End Using
        Else
            MsgBox("World.properties is not found")
        End If
    End Sub


But I keep getting an error saying world.properties is being used by another process.

Any help would be appreciated!

EDIT: Ok, well I sorta know why I get the error. It's in use twice so the second time is when it is getting the error. So I tried writing the edited file to world2.properties, and its just the same file. The edit isn't being made.

Any idea why? And I'd still like to have a better of editing the file instead of writing to a knew file, then deleting the original, then renaming the edited to the original's name xD

This post has been edited by animedude123: 20 Aug, 2008 - 08:34 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 10:30PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month