3

我如何阅读 Visual Studio 2012 C# 中的第二行文本

         the txt file

         user123
         **12345**
         asdfd

我想在一个 button1_click 中获得第二行并将其显示给 textblock2

我确实尝试从这里学习 如何读取文本文件中的指定行?

和这里 如何跳过第一行并从 C# 中的第二行开始读取文件

但是这些都不起作用,因为它无法在我的代码中应用

有什么帮助吗?

==================================================== =============================== 很抱歉让大家感到困惑,实际上我真的缺乏编程经验,我几乎不知道如何使用现在
我在windows8中使用vs2012,这是否意味着我在winrt中编码?

顺便说一句,我感谢您的所有帮助并成功地将答案应用于我的代码这是实际代码

        var file = await ApplicationData.Current.LocalFolder.GetFileAsync(tb1.Text+".txt");
        var line = await FileIO.ReadLinesAsync(file);
        if (tb2.Text == line[2])
        {
            tb3.Text = (line[1]);
        }
4

3 回答 3

5
var desiredText = File.ReadLines("C:\myfile.txt").ElementAt(1);

File.ReadLines()返回文件中行的 String[]。第二行的索引是 1。看这个

于 2012-12-01T11:29:03.703 回答
2

尝试

var desiredText = File.ReadLines("C:\myfile.txt");
textbox1.text = desiredText[1];
于 2012-12-01T11:39:32.847 回答
1

只需在开始捕获文件内容之前调用 .ReadLine() 即可。

从本质上讲,这将使读者跳过文件的第一行,只看第二行和它后面的所有行。

// Try this to take the second line.
string line;
using (var file_read = new StreamReader(your_file))
{
    file_read.ReadLine(); 
    line = file_read.ReadLine();
}
textBox1.Text = line.ToString();
于 2017-07-17T11:17:35.703 回答