How to skip first line and start reading file from second line in C#

前端 未结 3 1306
闹比i
闹比i 2021-01-14 15:30

How to start reading file from 2nd line skipping 1st line. This seems to work but is it best way to do so?

            using (StreamReader sr = new StreamRea         


        
3条回答
  •  甜味超标
    2021-01-14 16:14

    Could you not just read the first line outside of the loop without assigning it to a variable?

    using (StreamReader sr = new StreamReader(varFile, Encoding.GetEncoding(1250))) {
                string[] stringSeparator = new string[] { "\",\"" };
                if (!sr.EndOfStream)
                    sr.ReadLine();
                while (!sr.EndOfStream) {                    
                    string line = sr.ReadLine(); //.Trim('"');
                    string[] values = line.Split(stringSeparator, StringSplitOptions.None);
    
                    for (int index = 0; index < values.Length; index++) {
                        MessageBox.Show(values[index].Trim('"'));
                    }
                }
            }
    

提交回复
热议问题