- Home /
How To Update a Specific Line with StreamWriter?
How To Update a Specific Line with StreamWriter?
Hey Guys... I've got a problem:
I can't update a specific line without eraseing everything that was written before... And nobody seems to ask this question...
Is that even possible?
I ussing c#, so please if you can write your code in c#..
Thank you very much!
This question is not Unity-specific. You will find the answer by googling title of your question but with prefix "C#". Good luck.
Answer by ShadyProductions · Jul 22, 2017 at 11:44 PM
use the string.Replace method if you know what you need to replace but you'll have to rewrite the entire text once you replaced your line.
also look: https://stackoverflow.com/questions/1971008/edit-a-specific-line-of-a-text-file-in-c-sharp
Bruce Afruz's answer is maybe a better alternative for you
static void lineChanger(string newText, string fileName, int line_to_edit)
{
string[] arrLine = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612130906im_/https://answers.unity.com/themes/thub/images/avi.jpg)