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

To accomplish this task, you can use the following approach:

  1. Use OpenFileDialog class to select the csv file.
  2. Read the csv file line by line and add a new column for each line called "Situation" with either "Yes" or "No" value.
  3. Write the updated csv file to the destination folder using StreamWriter class.

Here's a sample code that demonstrates this approach in C#:

main.cs
using System.IO;
using System.Windows.Forms;

string csvFilePath = "";

// Select the CSV file using OpenFileDialog class
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    openFileDialog.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        csvFilePath = openFileDialog.FileName;
    }
}

// Read the CSV file line by line, add a new column and write to a new file
string destinationFolderPath = "<your_destination_folder_path>";
string newCsvFilePath = Path.Combine(destinationFolderPath, Path.GetFileNameWithoutExtension(csvFilePath) + "_updated.csv");

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

            // Add new column 'Situation' with Yes/No value
            bool situation = /* your logic to determine Yes/No */;
            string newLine = string.Format("{0},{1}", line, situation ? "Yes" : "No");

            writer.WriteLine(newLine);
        }
    }
}
1210 chars
39 lines

Note: You'll need to implement your own logic to determine the "Situation" value for each line. Also, make sure to provide valid folder and file paths so that the code works as expected.

gistlibby LogSnag