I'm trying to write a text file, from a datatable, text file which later on will be imported by some other application, therefor I need to have no empty lines in the exported file (at the end or between). I used the following code:
DataTable table = FisierSoldata.dtSoldata;
var result = new StringBuilder();
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(row[i].ToString());
result.Append(i == table.Columns.Count - 1 ? "\n" : " ");
}
result.AppendLine();
}
StreamWriter objWriter = new StreamWriter(filePath, false);
objWriter.WriteLine(result.ToString());
objWriter.Close();
My datatable has 26 records and when the file is generated, it has one dataline followed by one empty line and another dataline and so on and at the end after the last dataline it has 3 empty lines. How can I modify the code in order to have only datalines without the empty one between data and without the last 3 empty ones? Regards,
LE:I've found a workaround, by reading some similar thread, but the solution was to use both streamwriter and streamreader in order to remove the unneeded data (empty lines) from the file which will be generated. But I wanted to know if there's a way of getting the file as needed from the start.