select csv file and for each line add a column with yes or no and move the new csv to destination in csharp

csharp, file-handling, csv, data-manipulation, streamwriter

main.cs

To accomplish the task, we can use the `System.IO` namespace to read the CSV file, manipulate the data by adding a new column with "Yes" or "No", and write the updated data to a new CSV file in the destination folder.

Here's a sample C# code that demonstrates how to achieve this:

```csharp
using System;
using System.IO;
using System.Text;

namespace CsvManipulationExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFilePath = @"C:\path\to\source\file.csv";
            string destinationFolder = @"C:\path\to\destination\folder";
            string destinationFilePath = Path.Combine(destinationFolder, "newfile.csv");

            using (var reader = new StreamReader(sourceFilePath))
            using (var writer = new StreamWriter(destinationFilePath))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    var values = line.Split(',');

                    // Manipulate CSV data by adding new column
                    string newColumnValue = GetNewColumnValue();
                    Array.Resize(ref values, values.Length + 1);
                    values[^1] = newColumnValue;

                    // Write updated data to new CSV file
                    var newLine = string.Join(',', values);
                    writer.WriteLine(newLine);
                }
            }
        }

        static string GetNewColumnValue()
        {
            // Perform your Yes/No check here
            bool yes = true;
            return yes ? "Yes" : "No";
        }
    }
}
1634 chars
49 lines

Note that in the above code, we are using a StreamReader and StreamWriter to read and write data from and to the CSV files respectively. We are also manipulating the CSV data by adding a new column with either "Yes" or "No" based on some logic inside the GetNewColumnValue method. Once the data is updated, we write it to a new CSV file in the destination folder using the StreamWriter.

related categories

gistlibby LogSnag