user466663
user466663

Reputation: 845

How to avoid adding a blank line in file-IO using C#

I got help from this site to write data from objects into tab delimited text files. It works great. The problem is it puts an extra blank line at the end of the file. Because of this a process which picks up the file fails. Please let me know if there is a way to block the code putting the extra line. The following is the code:

private void WriteFile<T>(string filePath, IEnumerable<T> objectlist, string userName, string password)
{

        bool createHeader = false;

        if (!File.Exists(filePath))
        {
            using (File.Create(filePath)) ;
            createHeader = true;
        }

        string data = ToCsv<T>("\t", objectlist, createHeader);
        var file = new StreamWriter(filePath, true);
        file.WriteLine(data);
        file.Close();

}

Upvotes: 0

Views: 1234

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48279

Try file.Write instead of file.WriteLine.

Upvotes: 9

Related Questions