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

To achieve this, you can use the following steps:

  1. First, you need to add the System.IO namespace for performing input/output operations on files:
main.cs
using System.IO;
17 chars
2 lines
  1. Next, you need to use the OpenFileDialog class to allow the user to select the CSV file:
main.cs
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
    openFileDialog.InitialDirectory = "C:\\";
    openFileDialog.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
    openFileDialog.FilterIndex = 1;
    openFileDialog.RestoreDirectory = true;

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        string filePath = openFileDialog.FileName;
        
        // Perform operations on selected file
    }
}
442 chars
15 lines
  1. Inside the if block, you can read the contents of the CSV file using the StreamReader class and loop through each line:
main.cs
using (StreamReader reader = new StreamReader(filePath))
{
    using (StreamWriter writer = new StreamWriter(File.Create("destination.csv")))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            // Perform operations on each line
            string newLine = $"{line},yes"; // Add "yes" to the end of each line
            writer.WriteLine(newLine); // Write the modified line to a new file
        }
    }
}
456 chars
14 lines
  1. Finally, you can create a new file in the desired destination folder using the StreamWriter class and write the modified data to it.

Note: It's important to use the using statement when working with files in C# to ensure that the resources are disposed of properly when the operation is complete.

gistlibby LogSnag