protected void PublicAction(string slowStr)//slowStr就是你想写入的文本
{
FileStream fs = new FileStream(@"F:\log.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: " + slowStr + " " + DateTime.Now.ToString() + "\n\n");//写入文本
sw.Flush();
sw.Close();
fs.Close();
}
{
FileStream fs = new FileStream(@"F:\log.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("WindowsService: " + slowStr + " " + DateTime.Now.ToString() + "\n\n");//写入文本
sw.Flush();
sw.Close();
fs.Close();
}

public stringPublicActionRead(string path)
{
{
StreamReader sr = new StreamReader(path, Encoding.Default);
String str;
while ((str= sr.ReadLine()) != null)
{
str+=str;
}
String str;
while ((str= sr.ReadLine()) != null)
{
str+=str;
}
return str;//str就是读取出来的所有文本
}
}
本文档详细介绍了如何使用C#语言进行记事本文本的读写操作,包括打开、创建、追加和修改文本文件,以及处理文件路径和异常处理的基本方法。通过对示例代码的解析,读者将能够掌握C#在日常文本处理中的实用技巧。
639

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



