在C#中读写文件可以使用System.IO命名空间中的类和方法。以下是一些常用的方法:
using System.IO;
string path = "file.txt";
using (StreamReader sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
using System.IO;
string path = "file.txt";
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine("Hello, World!");
}
using System.IO;
string path = "file.bin";
using (BinaryReader br = new BinaryReader(File.Open(path, FileMode.Open)))
{
int length = (int)br.BaseStream.Length;
byte[] data = br.ReadBytes(length);
}
using System.IO;
string path = "file.bin";
using (BinaryWriter bw = new BinaryWriter(File.Open(path, FileMode.Create)))
{
byte[] data = new byte[] { 0x01, 0x02, 0x03, 0x04 };
bw.Write(data);
}
推荐的腾讯云相关产品和产品介绍链接地址:
以上是我的回答,如果您有任何问题,请随时问我。
领取专属 10元无门槛券
手把手带您无忧上云