StreamWriter replace line with a new text

前端 未结 1 741
忘了有多久
忘了有多久 2020-12-17 02:16

Is it possible to replace the text in a text file with a new text without erasing the other data, here is my sample code, but its not working, I know there\'s a problem with

相关标签:
1条回答
  • 2020-12-17 02:49

    You could do it a lot more easily:

            string[] lines = System.IO.File.ReadAllLines("test");
            lines[0] = /* replace with whatever you need */
            System.IO.File.WriteAllLines("test", lines);
    

    hope this helps

    also I'd suggest using int.TryParse if you don't want an exception to be raised in your portion of code in case the first line of the file or the textbox values aren't numeric

    if you really want to use the streamwriter you could go with this, also a simpler way:

    line[0] = newbal.ToString();
    foreach(string s in lines)
        sw.WriteLine(s);
    
    0 讨论(0)
提交回复
热议问题