C# 读取文件内容
以下是使用C#读取文件内容的示例代码:
using System;
using System.IO;
class Program
{
static void Main()
{
string filePath = "path/to/file.txt";
try
{
// 读取文件内容
string content = File.ReadAllText(filePath);
// 输出文件内容
Console.WriteLine(content);
}
catch (FileNotFoundException)
{
Console.WriteLine("文件不存在");
}
catch (IOException)
{
Console.WriteLine("读取文件时发生错误");
}
}
}
在上述代码中,首先需要指定要读取的文件路径,将其赋值给filePath变量。然后使用File.ReadAllText方法读取文件内容,并将结果赋值给content变量。最后,使用Console.WriteLine方法输出文件内容。
请确保将path/to/file.txt替换为实际的文件路径
原文地址: https://www.cveoy.top/t/topic/iGxT 著作权归作者所有。请勿转载和采集!