1.判断txt文本是否存在:
File.Exists(文本路径)
2.按行读取文本内容:
string[] lines=File.ReadAllLines(文本路径)
3.写文本
public void writeText(string txtPath,string context)
{
FileStream fs;
if(Directory.Exists(txtPath))
{
fs=new FileStream(txtPath,FileMode.Truncate,FileAccess.ReadWrite);
}
else
{
fs=new FileStream(txtPath,FileMode.Create);
}
StreamWriter sw=new StreamWriter(fs);
sw.Write(context);
sw.Flush();
sw.Close();
fs.Close();
}
本文介绍了使用C#进行文件操作的基本方法,包括判断文件是否存在、读取文本内容以及如何写入文本。通过示例代码展示了如何创建或截断文件,以及使用StreamWriter进行文本写入的过程。
3013

被折叠的 条评论
为什么被折叠?



