从文本文件中提取数据

我有一个文本文件,如下所示:

[Details] Version=100 Switch=340 Video=800 Date=20100912 Length=00:49:34.2 Days=1 hours=20 

有没有办法可以检查[Details]部分中的每一行,然后执行类似的操作?

 if(line == "version") { // read the int 100 } 

此外,文本文件中还有其他文本对我不感兴趣。 我只需找到[Details]部分。

  using (StreamReader sr = new StreamReader(path)) { while (sr.Peek() >= 0) { var keyPair = sr.ReadLine(); var key = keyPair.Split('=')[0]; var value = keyPair.Split('=')[1]; } } 

您可以使用File.ReadAllLines查找带有Version的行,然后从那里解析int

 foreach (var line in File.ReadAllLines("file").Where(line => line.StartsWith("Version"))) { int value = 0; if (int.TryParse(line.Replace("Version=","").Trim(), out value)) { // do somthing with value } } 

或者,如果文件只包含1行版本

 string version = File.ReadAllLines("file").FirstOrDefault(line => line.StartsWith("Version=")); int value = 0; if (int.TryParse(version.Replace("Version=", "").Trim(), out value)) { // do somthing with value }