你可以使用 StreamReader 类来读取 txt 文件的每一行数据。以下是示例代码:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    
    {
        string filePath = 'path/to/your/file.txt';

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

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

你只需要将 filePath 替换为你的 txt 文件的路径即可。这段代码将逐行读取文件并将每一行打印到控制台上。

C# 读取 TXT 文件每一行数据 - 简单示例

原文地址: https://www.cveoy.top/t/topic/mA1x 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录