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 9177313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:17:50+00:00 2026-06-17T17:17:50+00:00

I am reading a file using streamreader opened in ReadWrite mode. The requirement I

  • 0

I am reading a file using streamreader opened in ReadWrite mode. The requirement I have is to check for a file for specific text, and if it is found, replace that line with a new line.

Currently I have initialized a StreamWriter for writing.

It is writing text to a file but it’s appending that to a new line.

So what should I do to replace the particular line text?

System.IO.FileStream oStream = new System.IO.FileStream(sFilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read); 
System.IO.FileStream iStream = new System.IO.FileStream(sFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); 

System.IO.StreamWriter sw = new System.IO.StreamWriter(oStream);
System.IO.StreamReader sr = new System.IO.StreamReader(iStream); 

string line;
int counter = 0;
while ((line = sr.ReadLine()) != null)
{
    if (line.Contains("line_found"))
    {
        sw.WriteLine("line_found false");
        break;
    }
    counter++;
}
sw.Close();
sr.Close();
  • 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-17T17:17:51+00:00Added an answer on June 17, 2026 at 5:17 pm

    Hi Try the below Code…. it will help you….

    //Replace all the HI in the Text file…

    var fileContents = System.IO.File.ReadAllText(@"C:\Sample.txt");
    
    fileContents = fileContents.Replace("Hi","BYE"); 
    
    System.IO.File.WriteAllText(@"C:\Sample.txt", fileContents);
    

    //Replace the HI in a particular line….

            string[] lines = System.IO.File.ReadAllLines("Sample.txt");
            for (int i = 0; i < lines.Length; i++)
            {
                if(lines[i].Contains("hi"))
                {
                    MessageBox.Show("Found");
                    lines[i] = lines[i].Replace("hi", "BYE");
                    break;
                }
            }
            System.IO.File.WriteAllLines("Sample.txt", lines);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading in from a text file using StreamReader into a string array
I'm reading a file using bufferedreader, so lets say i have line = br.readLine();
I am reading one file using ruby in which I have written regexp in
I have .zip file which contain csv data. I am reading .zip file using
i am reading an excel file using this code i found over the net:
I have a file at a URI that I would like to read using
Friends, I have a requirement i am given a text file with fixed no.
The StreamReader locks a text file whilst it is reading it. Can I force
I am reading from a text file line by line. StreamReader reader = new
I have a library that handles reading and writing a cache file. This library

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.