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

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?
 
C

Carl Daniel [VC++ MVP]

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
 
M

Michael Rubinstein

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
 
D

daniel.cigic

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top