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
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:
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.Equalswithline.StartsWithif you wish to skip all lines that start with the given text.Given this input file:
It will produce this output:
NOTES
In your example you used this code inside the
whileloop: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):