Append Lines to a File Using a Streamwriter

Append lines to a file using a StreamWriter

Use this instead:

new StreamWriter("c:\\file.txt", true);

With this overload of the StreamWriter constructor you choose if you append the file, or overwrite it.

C# 4 and above offers the following syntax, which some find more readable:

new StreamWriter("c:\\file.txt", append: true);

C# Using StreamWriter, append a string in a specific line

It can be a possible solution:

var input = File.ReadAllLines(filepath);
var output = new List<string>();

foreach (var line in input)
{
output.Add(line);

if (line == "firstTargetValue")
{
output.Add("firstNewValue");
}
}

output.ForEach(line => Console.WriteLine(line));

Append text using StreamWriter


using(StreamWriter writer = new StreamWriter("debug.txt", true))
{
writer.WriteLine("whatever you text is");
}

The second "true" parameter tells it to append.

http://msdn.microsoft.com/en-us/library/36b035cb.aspx

How to add line to text with StreamWriter?

There is a flag in the constructor overload to advocate the writer to append the new content at end of file. You can pass true as argument to append new content instead of replacing whole content:

new StreamWriter(appdata + @"\FILE.txt", append: true);

Using StreamWriter to append text

You should always put StreamWriter objects in a using statement so they get closed properly.

using (StreamWriter oFile = File.AppendText("baza.txt"))
{
string output = "Current date and time: "
+ DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss");
oFile.WriteLine(output);
}

Alternatively, you can manually call the Close method on the StreamWriter, but the using statement, to me, is much easier and less error-prone.

Filestream file and Streamwriter in foreach and add line when done

Currently you're creating a StreamWriter, writing to it, and disposing it for every list, this is what's causing the issue. Internally the Dispose method closes the underlying stream causing the exception. To solve this we can do one of 2 things

  1. Tell our StreamWriter to not close the underlying stream.
  2. Not dispose our StreamWriter until we're also done with the underlying stream.

Here's how to do #1:

Simply replace your call to the constructor with this

using (StreamWriter outputFile = new StreamWriter(fs, leaveOpen: true))

Here's how to do #2:

Move the using (StreamWriter ... block up to be "one level deeper" than the using (FileStream ... block

using (FileStream fs = new FileStream("..."))
{
using (StreamWriter outputFile = new StreamWriter(fs))
{
// Your foreach loops here
}
}

Personally I'd go with #2 as I'm not a fan of creating and disposing objects in a loop

How to append a new line to a file

Along with @metal answer, this shall do the job.

static void Main(string[] args)
{
string fileLoction = @"C:\project\user.txt";
StreamWriter WriteNewLine = new StreamWriter(fileLoction, true);
WriteNewLine = File.AppendText(fileLoction);
WriteNewLine.Close();
}


Related Topics



Leave a reply



Submit