溫馨提示×

C#如何讀取文件中部分數據

c#
小億
167
2024-01-12 02:26:02
欄目: 編程語言

在C#中,可以使用StreamReader類來讀取文件中的數據。以下是一個示例代碼,演示了如何讀取文件中的部分數據:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string filePath = "path_to_your_file"; // 替換為實際文件路徑

        try
        {
            using (StreamReader sr = new StreamReader(filePath))
            {
                // 讀取文件中的前10行數據
                int lineCount = 0;
                string line;
                while ((line = sr.ReadLine()) != null && lineCount < 10)
                {
                    Console.WriteLine(line);
                    lineCount++;
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("文件讀取錯誤: " + e.Message);
        }
    }
}

以上代碼會讀取文件中的前10行數據,并將每行數據打印到控制臺。你可以根據自己的需要修改代碼,以適應讀取文件中的其他部分數據。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女