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

  • SEARCH
  • Home
  • 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 7944819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:39:58+00:00 2026-06-04T00:39:58+00:00

I have my project writing to two text files. one for input and one

  • 0

I have my project writing to two text files. one for input and one for output.
I need them to at the end, be writing into both the same text file.

here is my code so far:

static void Main( string[] args )
    {
        string line = null;
        string line_to_delete = "--";
        string desktopLocation = Environment.GetFolderPath( Environment.SpecialFolder.Desktop );
        string text = Path.Combine( desktopLocation, "tim3.txt" );
        string file = Path.Combine( desktopLocation, "tim4.txt" );

        using (StreamReader reader = new StreamReader( text ))
        {
            using (StreamWriter writer = new StreamWriter( file ))
            {
                while (( line = reader.ReadLine() ) != null)
                {
                    if (string.Compare( line, line_to_delete ) == 0)
                        File.WriteAllText( file, File.ReadAllText( text ).Replace( line_to_delete, "" ) );
                    continue;
                }
            }

Thanks

  • 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-04T00:39:59+00:00Added an answer on June 4, 2026 at 12:39 am

    If you want to read all lines from the input files and write them all to the output file with the exception of lines that match a given text:

    public static void StripUnwantedLines(
        string inputFilePath,
        string outputFilePath,
        string lineToRemove)
    {
        using (StreamReader reader = new StreamReader(inputFilePath))
        using (StreamWriter writer = new StreamWriter(outputFilePath))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                bool isUnwanted = String.Equals(line, lineToRemove,
                    StringComparison.CurrentCultureIgnoreCase);
    
                if (!isUnwanted)
                    writer.WriteLine(line);
            }
        }
    }
    

    In this case the comparison is made using the current culture (it may do not be important if you need to search for “–” but it’s clear to specify) and it’s case insensitive.
    You may need to change String.Equals with line.StartsWith if you wish to skip all lines that start with the given text.

    Given this input file:

    This is the begin of the file
    --
    A line of text
    --
    Another line of text
    

    It will produce this output:

    This is the begin of the file
    A line of text
    Another line of text
    

    NOTES
    In your example you used this code inside the while loop:

    File.WriteAllText(file, 
        File.ReadAllText(text).Replace(line_to_delete, ""));
    

    It may be enough without anything else (but it’ll remove the unwanted lines replacing them with empty ones). Its problem (if to keep empty lines is not an issue) is that it’ll read the entire file in memory and it may be (very) slow if the file is really big.
    Just for information this is how you may rewrite it to do the same task (for not too big files because it works in memory):

    File.WriteAllText(outputFilePath, File.ReadAllText(inputFilePath).
        Where(x => !String.Equals(x, lineToDelete));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have project in Dropbox and two running laptops: one with Ubuntu and one
I'm writing a django project, and I need to have a parallel thread which
I am writing pom.xml for our project. I need to copy two different versions
So I have two copies of exactly the same project. The configuration of the
I have 9 xml files which is our project database, we are writing in
I joined a project yesterday, we decided to merge our two projects into one,
For a project I'm using both Scala and Riak (two things I have never
I have a multi-project ASP.NET + C# solution. I was planning on writing a
I'm currently writing a ToDo-Script for a project i have running. Now there is
I'm new to the world of Makefile writing. I have a C project which

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.