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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:41:30+00:00 2026-06-08T22:41:30+00:00

I would like to modify the following code to work with large files. public

  • 0

I would like to modify the following code to work with large files.

    public static void Replace(string filePath, string searchText, string replaceText)
    {
        StreamReader reader = new StreamReader(filePath);
        string content = reader.ReadToEnd();
        reader.Close();

        content = Regex.Replace(content, searchText, replaceText);

        StreamWriter writer = new StreamWriter(filePath);
        writer.Write(content);
        writer.Close();
    }

I’m thinking that I’m going to need to open a filestream to write to a new file name, and then delete the original file and replace it with the new one when I’m done. Does that sound about right?

Also…
I love the simplicity of this routine, that aside from the necessary file i/o lines of code there is only one line of code to process the file. However, I’m also wondering if I’m sacrificing performance for simplicity… is Regex.Replace very performant?

  • 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-08T22:41:32+00:00Added an answer on June 8, 2026 at 10:41 pm

    First: you can try Regex with Stream (it seems be more faster and less memory requirement):

    • Building a Regular Expression Stream Search with the .NET Framework

    or see Mono-Project Regex. It has Regex with streaming.

    see this article for Regex performance:

    • C# Regex Performance (from dotnetperls)

    or if using Regex is not necessary use String.Replace and try this one line code:

    File.WriteAllText(filePath, 
                      File.ReadAllText(filePath).Replace(searchText, replaceText));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code, similar to the following, that I would like to modify: Sub
I would like someone to modify the following code to save data to generate
I would like modify HTML like I am <b>Sadi, novice</b> programmer. to I am
I would like to modify an object private variable class Example(): __myTest1 = 1
We have a customer that would like to modify application user messages that we
I'm absolutely new to JavaScript and would like to modify a textarea of a
I'm using HighLine to write my console application and I would like to modify
I would like to select a node and modify its attributes and child-nodes using
I would like to load a HTML document and modify it's text in PHP.
I would like to retrieve the contents of a file, filter and modify them

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.