What is the easiest way to open a textfile for append or create it for text append?

  • Thread starter Thread starter Wolfgang Meister
  • Start date Start date
W

Wolfgang Meister

I would like to append text to a file which might exist or not.

What is the easiest way to open or create it depending if it exists or not
and to set it into an "append" text mode?
 
Wolfgang Meister said:
I would like to append text to a file which might exist or not.

What is the easiest way to open or create it depending if it exists or not
and to set it into an "append" text mode?

Just do it -

FileStream fs = new FileStream("your_path", FileMode.Append);

.... or were you looking for something else?

-cd
 
Something like this for a file with full path stored in 'path' variable:

StreamWriter writer = null;

if (File.Exists(path))

{

writer = new StreamWriter(path, true);

}

else

{

writer = new StreamWriter(File.Create(path));

}

writer.WriteLine("Nex line or whatever else"));

writer.WriteLine("Another line or whatever");

writer.Close();



Michael
 
I would like to append text to a file which might exist or not.

What is the easiest way to open or create it depending if it exists or not
and to set it into an "append" text mode?

I guess that:

File.AppendAllText(string path, string content);
 
Back
Top