I am reading in a CSV file in the format:
10009279,D002158,,"Mount Robinson deposit",38.1139,-105.34557,NA,"United States",Colorado,Custer,B,"Aluminum, Potassium",,
I would like to write out a new CSV file based on only the selected columns from the initial CSV file, so the resulting structure would look like:
-105.34557,38.1139,"Mount Robinson deposit","Custer "Aluminum, Potassium"
I have tried:
StreamWriter writer = new StreamWriter(@textBox2.Text);
string[] lines = File.ReadAllLines(@textBox1.Text);
foreach (string line in lines)
{
string[] fields = line.Split(',');
writer.WriteLine(string.Format("{0},{1},{2},{3}", fields[LONcomboBox.SelectedIndex], fields[LATcomboBox.SelectedIndex], fields[NAMEcomboBox.SelectedIndex], fields[10 + 13]));
}
writer.Close();
Which works with the following problems:
- commas embedded in double quotes don't seem to be handled correctly
- concatonating multiple fileds seems to fail (this is resolved thanks for the answer)
any suggestions would be appreciated!
I have also attempted to use FileHelpers but can't seem to get it to work using the index of the column.