Linux 拨号vps windows公众号手机端

c#如何逐行读取txt文件数据

lewis 8年前 (2017-10-07) 阅读数 6 #程序编程
文章标签 c#

在C#中,可以使用StreamReader类逐行读取txt文件数据。下面是一个示例代码:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "path_to_your_txt_file.txt";

        // 使用StreamReader打开文件
        using (StreamReader sr = new StreamReader(filePath))
        {
            string line;

            // 逐行读取文件数据
            while ((line = sr.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
        }
    }
}

在上述代码中,请将path_to_your_txt_file.txt替换为你要读取的txt文件的路径。然后,我们使用StreamReader类打开文件,并使用ReadLine()方法逐行读取数据。每次读取一行数据后,可以进行后续处理,这里我们简单地将每行数据打印到控制台上。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门