C# 如何在C语言中从文本文件中读取特定行

C# 如何在C语言中从文本文件中读取特定行,c#,C#,如何从C中的文本文件中读取例如第2行的特定行 abc.txt是包含的文件名 第1行-xyz 第2行-pqr 我想阅读abc.txt中的pqr,如果您能解释一下您的答案是如何工作的以及为什么工作的,这将对OP和整个社区非常有用。 string[] stringArray = System.IO.File.ReadAllLines("C:\\abc.txt"); string line1 = stringArray[0]; string line2 = stringArray[1];//This i

如何从C中的文本文件中读取例如第2行的特定行

abc.txt是包含的文件名 第1行-xyz 第2行-pqr


我想阅读abc.txt中的pqr,如果您能解释一下您的答案是如何工作的以及为什么工作的,这将对OP和整个社区非常有用。
string[] stringArray = System.IO.File.ReadAllLines("C:\\abc.txt");
string line1 = stringArray[0];
string line2 = stringArray[1];//This is the one you want
string line3 = stringArray[2];
string[] stringArray = System.IO.File.ReadAllLines("C:\\abc.txt");

// Array starts from 0 so basically the first position is 0.

// So I thought of a myb not bad idea
// You can say e.g. pqr is at the line no 2 and then you'll need to subtract - 1
// and that will be your index also rqy is on position 2 - 1 = index[1].

// Myb my explanation isn't the best but i've tried.

string line1 = stringArray[0]; // index 0
string line2 = stringArray[1]; // index 1
string line3 = stringArray[2]; // index 2