adding a line of text

in an existing text file

Page 1 of 1

5 Replies - 4608 Views - Last Post: 08 June 2011 - 02:18 PM Rate Topic: -----

#1 TADS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 161
  • Joined: 09-August 08

adding a line of text

Posted 28 January 2009 - 03:14 AM

hi again all,

im looking at a more open approach to the application i made few days ago...

i would like to use a "template" text file that would be stored in the app path in a "templates" folder the user then picks what template they want and enters the few lines of text they need to change in textbox1 and texbox2 on the main form... please dont think i want someone to write my code for me i would just like to know how to go about it and if there is a tutorial online or that someone has i would like to read it as i always find i learn alot more by reading a tutorial then just a block of code... this is what i have so far..

Dim filename As String = "C:\Documents and Settings\TADS\Desktop\kev\tads.veh"
		File.Open(filename, FileMode.Append)
		File.ReadAllLines(filename)
		File.ReadAllText(filename)
		If filename.Contains("DefaultLivery=") Then
			filename.Replace("DefaultLivery=", "DefaultLivery=" & Chr(34) & TextBox1.Text & Chr(34))
			File.CreateText(filename)

		End If



now i know i need to add something like...


File.WriteAllLines(filename, contents)



but somewhere along the lines i have lost myself and i havn't set the content (the string array to write the file) im not sure how to go from here..

or even if this is the correct way to do the job in question

thanks for reading

Kev

Is This A Good Question/Topic? 0
  • +

Replies To: adding a line of text

#2 TADS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 161
  • Joined: 09-August 08

Re: adding a line of text

Posted 28 January 2009 - 01:34 PM

i have come up with a way to wright to the textfile but it wasnt what i wanted in the end as i need to delete that line of text before i can write to it the code i have is...

Dim Fs As FileStream = New FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
		Dim sw As New StreamWriter(Fs)
		Dim sr As New StreamReader(Fs)
		Dim str As String
		Dim old As String
		Dim new1 As String
		old = "oldtext"
		new1 = "newtext" & Chr(34) & TextBox1.Text & Chr(34)
		Try
			str = sr.ReadToEnd()
			Fs.Position = 0
			Fs.SetLength(str.Length)

			If str.StartsWith(old) Then
				str = str.Replace(old, new1)
			End If
		Catch ex As Exception
			MsgBox("error" & (ex.Message))
		End Try

		sw.Write(Str)
		sw.Flush()
		sw.Close()
		Fs.Close()
	End Sub



this would be ok if the only word on that line is oldtext but i would never know how long the word will be (oldtext432234) or something...

so i tryed using
str = str.Remove(0, 10)


but as said before i dont know how long the line of text will be all i know it will contane oldtext + more

so is there a way i could split the readtoend and just delete that one whole line of code??

thanks for reading

Kev

ps: this is for a game when making the new car files!

This post has been edited by TADS: 28 January 2009 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 sam.adams61  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 283
  • Joined: 14-July 08

Re: adding a line of text

Posted 28 January 2009 - 06:45 PM

What you have written seems to me to be a bit ambiguous...not clear! If you want to enter new text, what are you doing to save the old? Why is there no apparant 'event' to save the new text, i.e. like a button click event? Then again, perhaps I'm completely misunderstanding you. However, I did try to run your code, but without success. Not being overly experienced myself, the line that reads
 Dim Fs As FileStream = New FileStream(Path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
tells me that 'Path' is a type and cannot be used as an expression. I'll be interested to learn how you get on with this. All the best, & sorry I'm unable to really help out.
Was This Post Helpful? 0
  • +
  • -

#4 TADS  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 161
  • Joined: 09-August 08

Re: adding a line of text

Posted 29 January 2009 - 06:09 AM

if you set a value to "path" then it will compile

ie...

Dim path As String = ("c:\test.txt")



then the filestream will know what "path" is

Kev

ps i am not "saving" the file its Replacing one word with another and i wanted to know if i can replace a line that starts with oldtext and put in my own line in its place... now i was told i could use indexof() but im unsure how to use it.... i did try
str = str.indexof(vbCrLf)



and this returns a value of 30 (that is the value of letters on that line) but it writes it to my text file and im not sure how to use it in full if someone could help me out would be brill....

also i tryed adding fileopen to save the text file in a different dir but it puts " at top of the text file and at the bottom and also if there is anything in the text file like

dir="c:\"

it put extra " in that line like

dir=""c:\""

thanks for reading
Was This Post Helpful? 0
  • +
  • -

#5 samuel17  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 209
  • Joined: 14-October 10

Re: adding a line of text

Posted 08 June 2011 - 01:50 PM

Hi TADS,
Im not sure if you ever figured this out, i know this is a really old topic, but i have been working a lot with text files lately. From the things i have done, the replace method you were using should work fine. What i did on one program is to read the text file in, assign all the text to a string variable, use the replace method as your first post had, then use the writealltext() method to write that back and save over your old text file, or create a new file. Hope this has helped some if in the past 2 years you still struggled :P

Sam
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: adding a line of text

Posted 08 June 2011 - 02:18 PM

I am afraid where did you get this 2 years old post and bring it here :nottalkingtoyou:
It was last updated before I joined here! I think there was no need to bring an old post like this to life
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1