【问题标题】:how to read specific line from the text file in C# [duplicate]如何从C#中的文本文件中读取特定行[重复]
【发布时间】:2019-07-03 10:11:34
【问题描述】:

如何在 C# 中从文本文件中读取特定的行(例如第 2 行)。

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

我想从 abc.txt 读取 pqr

【问题讨论】:

    标签: c#


    【解决方案1】:
    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];
    

    【讨论】:

    • 如果您能解释一下如何为什么您的答案有效,这将对 OP 和整个社区很有用。
    【解决方案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
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      相关资源
      最近更新 更多