It is necessary to read with the help of StreamReader a .csv file. There are a lot of lines in the file. Each line has five values ​​separated by commas. I use for this

 private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "Cursor Files|*.csv"; openFileDialog1.Title = "Выберите ранее записанный график в формате .csv"; if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName); data1_read =sr.ReadLine(); string[] slist = data1_read.Split(','); sr.Close(); data_temp_in.Add(slist[0]); foreach (var temp_in in data_temp_in) { MessageBox.Show(Convert.ToString(temp_in) + ""); } } 

But, as I understand it, I read only the first line, since there are no other values ​​in the MessageBox . (only a zero value appears, which was read from the file).

How can I count columns from a file with multiple rows, add them to the Collection and then build a graph from this collection?

It used to be easier - we have two arrays. One is the values ​​of X, the second values ​​of Y, and everything, on the basis of these data we build a graph, but here it is not clear at all.

  • What kind of graph are you building with the library? What does the code look like for graphics when you work with arrays A and B? - pavelip

2 answers 2

The file can be read and displayed in the MessageBox as follows:

 using System.Windows.Forms; private void button1_Click(object sender, EventArgs e) { var fileName = GetFileNameFromDialog(); if (fileName != null) { var list = ReadLines(fileName); foreach (var cells in list) { MessageBox.Show(string.Join(" ", cells)); } } } private DialogResult GetFileNameFromDialog() { var dialog = new OpenFileDialog(); dialog.Filter = "Cursor Files|*.csv"; dialog.Title = "Выберите ранее записанный график в формате .csv"; return dialog.ShowDialog() == DialogResult.OK ? dialog.FileName : null; } private List<string[]> ReadLines(string fileName) { var list = new List<string[]>(); var lines = File.ReadAllLines(); foreach(var line in lines) { var cells = line.Split(','); list.Add(cells); } return list; } 

UPDATE

You can write to the list not only with an array of strings. We describe the class and change the method of reading from the file:

 public class DataRow { public string In; public string Out; public string Irrigation; public string TempTrigger; public string VpsRisk; } private List<DataRow> ReadLines(string fileName) { var list = new List<DataRow>(); var lines = File.ReadAllLines(); foreach(var line in lines) { var cells = line.Split(','); var item = new DataRow(); item.In = cells[0]; item.Out = cells[1]; item.Irrigation = cells[2]; item.TempTrigger = cells[3]; item.VpsRisk = cells[4]; list.Add(cells); } return list; } //... MessageBox.Show(cells.In + " " + cells.Out + " " + cells.Irrigation); // итд //... 

    Specifically, your case - you read only one line, and you need to read them all and process them in a loop. For example:

     IEnumerable<string> lines = File.ReadLines(openFileDialog1.FileName); foreach (var line in lines) { string[] slist = line.Split(','); ... 

    In addition, the csv format is not so simple, and you'd better parse it not with your hands, but with a parser .