Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:41:11+00:00 2026-06-17T19:41:11+00:00

Is it possible to replace the text in a text file with a new

  • 0

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 it but I can’t figure out, thanks,

private void button1_Click_1(object sender, EventArgs e)
{
    StreamReader sr = new StreamReader("test10101.txt");
    List<string> lines = new List<string>();
    while (!sr.EndOfStream)
        lines.Add(sr.ReadLine());
    output = Convert.ToInt32(textBox1.Text);
    newbal = Convert.ToInt32(lines[0]) - output;
    MessageBox.Show("Please get your cash....\n\nYour new balance is: $" + newbal);
    sr.Close();
    {
        string linetoreplace = lines[0];
        int newlinevalue = newbal;
        string contents = sr.ReadToEnd();

        StreamWriter sw = new StreamWriter("test10101.txt" + ".tmp");
        //contents = Regex.Replace(contents, linetoreplace, newlinevalue.ToString());
        contents = contents.Replace(linetoreplace, newlinevalue.ToString());
        sw.WriteLine(contents);
        sw.Close();

    }

I’m wondering if I use the Regex or directly replace the line,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:41:12+00:00Added an answer on June 17, 2026 at 7:41 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Editing a text file in place through C# I need to replace
Is it possible to replace text without having to create a TAG? I'm using
Possible Duplicate: Find and Replace Inside a Text File from a Bash Command I've
Is it possible to replace text within a pdf file using Google Apps Script?
Possible Duplicate: Replace first line of a text file in Java Java - Find
Possible Duplicate: How can I replace lines in a text file with lines from
Is it possible to replace part of text or HTML file with PHP? I'm
Is it possible to find and replace in selected text in Snow Leopard Xcode
Possible Duplicate: Replace words in a string, but ignore HTML Is it possible to
Possible Duplicate: How to append text to an existing file in Java I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.