select csv file and for each line add a new column called situation with any thig and move the new csv to destination in csharp

main.cs
using System;
using System.IO;
using System.Linq;

namespace CsvManipulation
{
    class Program
    {
        static void Main(string[] args)
        {
            // csv file path
            string filePath = @"C:\example\input.csv";

            // read all lines from csv
            string[] csvLines = File.ReadAllLines(filePath);

            // header of new column
            string newColumnHeader = "Situation";

            // adding new column header to existing header row
            string[] headers = csvLines.First().Split(',');
            string[] newHeaders = headers.Append(newColumnHeader).ToArray();

            // adding data to new column for each row
            string[] newData = csvLines.Skip(1)
                                       .Select(line =>
                                       {
                                           string[] cols = line.Split(',');
                                           string situationData = "Something"; // setting value for new column
                                           string[] newCols = cols.Append(situationData).ToArray();
                                           return string.Join(',', newCols);
                                       })
                                       .ToArray();

            // combining new headers and data to form a new csv
            string newCsv = string.Join(Environment.NewLine, newHeaders.Concat(newData));

            // writing new csv to destination path
            string destinationPath = @"C:\example\output.csv";
            File.WriteAllText(destinationPath, newCsv);
        }
    }
}
1626 chars
44 lines

gistlibby LogSnag