Hey there,

So I've been searching for method to replcace on particular line in an UTF-8 (easily readable and copy-paste) file.
I found lots of methods that were searching for matches in rows and replacing them.
It was mentioned very often that it's impossible to replace a row in text file using C#, unless they're "accidentally" same size.
Mine will be, it will be a file with constant length 200~300 of on each row, repeating vast amount of times 2000~???.

Could someone please provide me (link with) script that would replace row 12 to for example PLPLPPLPLPL (this test file will be just that short), so I can evaluate the script and modify it to my purposes and most likely learn from it. Problem is, it needs to be able to replace the row regardless what's on it. The length of each and single one row will be exactly same.

Recommended Answers

All 2 Replies

Oh, I forgot to mention, there are possbilities to do that. But what they do is entirely read the whole file, replace the row in an array, and then write it, but it would be big anti-boost down to performance, given that the file would have to be used multiple number of times.

Use the FileStream.Seek method to position yourself on the line to be removed. Multiply your linelength with the lineNr to be removed minus one to postion yourself in the file just before the line to be removed. Now write the new line.
Success.

commented: If every single answer was given like that, I'd be C# professional by now! Great answer, gonna try it. +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.