My project is currently spposd to delete all thetext after the — signs, as it represents a comment.
my code now is deleting all the text in the text in the text file, een before the comment.
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;
}
}
how can i specify that it must only delete the te
Thanks
The problem in your code is the fact that the replace (and this is the only instruction writing to your output file) would happen only when the file contains a line which is exactly equal to “–“.
Furthermore you don’t need the while loop if you are using WriteAllText and ReadAllText, and you can’t use them anyway since in this way you would only delete the “–” and not everything that comes after that.
I think that something like this should work: