C#从csv

时间:2019-09-26 10:25:08

标签: c# csv

我有这样的CSV文件:

field1,field2,field3,field4,...,fieldN
1,2,3,,...,N

,我想准确提取第二行的第14条记录。 我已经尝试过以下代码:

string nomeFile = "externalSourceMsg " + this.DataSource.CodSorgEst + this.DataSource.CodIdSorgEst + ".csv";
string content = "Field1,Field2,Field3,Field4,Field5,Field6,Field7" + Environment.NewLine;
content += externalMessage.ToString();
content = content.Replace(',', ';');

int i = 0;
while (content){
    int field;
    if(i++ == 13)
        field = content+i;
}

但是它不起作用。我做错了什么?

2 个答案:

答案 0 :(得分:5)

分割字符串,您可以按索引访问字段。

// Read all lines and get the second (this can be done 
// in more optimal way, just by reading two first lines)
var secondLine = File.ReadLines("Path")[1];
// Split byb comma and get 14th item in returned array
var entry = secondLine.Split(",")[13];

答案 1 :(得分:1)

尝试:

error