c#怎么调用txt数据
时间: 2023-11-24 13:44:24 浏览: 43
在 C# 中调用文本文件的数据,可以使用 System.IO 命名空间中的 StreamReader 类。下面是一个示例代码,演示如何读取文本文件的内容:
```csharp
using System;
using System.IO;
class Program
{
static void Main()
{
// 指定文本文件的路径
string filePath = "path/to/your/file.txt";
// 使用 StreamReader 读取文本文件
using (StreamReader sr = new StreamReader(filePath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
// 处理每一行的数据
Console.WriteLine(line);
}
}
// 程序执行完毕后,按任意键退出
Console.ReadKey();
}
}
```
请将上述代码中的 "path/to/your/file.txt" 替换为你实际的文本文件路径。这段代码会逐行读取文本文件的内容,并将每一行打印到控制台上。你可以根据需要对每行数据进行进一步的处理。