Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

StreamWriter replace line?

streamwriter stream

  • Please log in to reply
1 reply to this topic

#1 cypro

cypro

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 14 August 2011 - 12:15 AM

Hello to evreyone i am using Visual Studio 2008 for native program project.

I am using StreamReader and StreamWriter to read and write to file my question is, how can i replace a line with new value without change line.

e.g myfile.txt
blue
red
yellow

and to change to
green
red
yellow

The value is not the same but i want to replace and not to delete and writer new because is organized by ID!
Thank you
  • 0

#2 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 18 August 2011 - 09:15 AM

First you need to read the contents of the file line by line. Then if it contains a certain text replace it with your new text.

ReadFileByLine
If Line.Contains("Blue")
Line = Line.Replace("Blue", "Green")

The above code is obviously not a working example but I hope you get the point. :)

~ Committed.
  • 0
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download