'C# delete each row in a big CSV file that contains a specific value in a specific column [duplicate]

I'm working on a Unity application and I have a csv file with 6 columns and 5000+ rows. The files might grow significantly. The second column contains either "0" or "1". All rows with a "0" are not needed.

I want to read the CSV file and write every row that has no "0" at the 2nd column into a new csv file, so that I have a new CSV file with only rows that where 1. (The "1" also doesn't need to be in the new CSV file then.)

enter image description here

I tried something like this, but I removed the parts that didn't work. Maybe someone has a idea. Mainly the index x and i where somehow off. Sometimes it didn't read the first column but I might just have confused myself with the incrementation.

    public void UpdateCSV()
    {
        string[] values = File.ReadAllText(datapath).Split(new char[] { ',' });
        StringBuilder ObjStringBuilder = new StringBuilder();
        
        // x as index for second column
        int x = 1;
        for (int i = 0; i < values.Length; i++)
        {

            // if column 2 is not 0 it needs to be printed in the new file
            if (values[x] != "0")
            {
                //using ObjStringBuilder.Append() to pass the values of the row that I want to keep

            }

        }

        ObjStringBuilder.ToString().Remove(ObjStringBuilder.Length - 1);
        File.WriteAllText("Assets/Datasets/UpdatedCSV.csv", ObjStringBuilder.ToString());
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source