Skip to content

How can we use AppendAllLines in .net framework 3.5

Aditya Chauhan

Aditya Chauhan

Asked May 18, 2015, 9:32 PM EDT2 answers
Share
LinkedInX
AppendAllLines is work in .net framework 4.5
but how can we use append all line in 3.5

//4.5
string[] a = new string[] { fa + " "+ lastModified };
System.IO.File.AppendAllLines(filepath, a);

//3.5
string[] a = new string[] { fa + " "+ lastModified };
var ab = string.Join(Environment.NewLine, DataList.Select(x => x.blah blah blah).ToArray());
string outputFilePath = @"C:\output.txt";
File.AppendAllText(outputFilePath, ab);

public static void AppendAllLines(string path, IEnumerable<string> lines)
{
using (var writer = new StreamWriter(path, true))
foreach (var line in lines)
writer.WriteLine(line);
}

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    You can't use File.AppendAllLines() function in version prior to .NET 4.0. It only works in version 4.0 & above.

    Instead what you can do is create a custom AppendAllLines() function easily to solve your problem. In the following sample code we are iterating through each line and appending them one by one to the file specified using "filePath" variable.

    Example:

    C#
    //Function definition
    public static void AppendAllLines(string filePath, IEnumerable<string> inputLines)
    {
        using (var writer = new StreamWriter(filePath, true))
            foreach (var line in inputLines)
                writer.WriteLine(line);
    }
    - See more at: http://findnerd.com/list/view/AppendAllLines-function-in--Net-framework-3-5-or-similar-/3117/
    0
  • Manoj Bhoir
    0
    I did not understand why you have post this question because you already know the answer of your question. :)
     
    Nothing in the framework exists to do exactly that, using the proper streaming, but you can write your own method easily enough:
    C#
    public static void AppendAllLines(string path, IEnumerable<string> lines) 
    {     
       using (var writer = new StreamWriter(path, true)) 
          foreach (var line in lines)             
             writer.WriteLine(line); 
    }

    How to Use :

    C#
    string[] a = new string[] { fa + " "+ lastModified }; // Your array of String with new lines to add into file.
    AppendAllLines(filepath, a);

    Code
    0