c# 读写文件

使用c#读写txt文件,记录Log。

参考链接:

http://www.runoob.com/csharp/csharp-file-io.html

http://www.runoob.com/csharp/csharp-text-files.html

http://www.runoob.com/csharp/csharp-binary-files.html

举例
    StreamWriter writeLog;
    writeLog = new StreamWriter("log.txt");
    writeLog.WriteLine("hello world");
    writeLog.Flush();   //清空缓存,否者文件的内容不会实时更新
    writeLog.close();

    StreamReader readLog 
    readLog = new StreamReader("log.txt"))
    string line;
    line = sr.ReadLine();
    Console.WriteLine(line);
    readLog.close();

streamwriter对于文件属性的设置方法不是很多,混合FileStream使用。

     StreamWriter writeLog;
     StreamWriter writeTempLog;
     StreamReader readTempLog;
     FileStream Log;

     Log = new FileStream("log.txt", FileMode.Create, FileAccess.ReadWrite,FileShare.ReadWrite);
     writeLog = new StreamWriter(Log);

     readTempLog = new StreamReader(LogTemp);
     string logData;
     logData = readTempLog.ReadLine();
     while ((logData = readTempLog.ReadLine()) != null)
     {
         writeLog.WriteLine(logData);
         writeLog.Flush();
     }
     readTempLog.Close();
     writeLog.Close();
     Log.Close();
Author:

Tony Liu

2016-7-4, Shenzhen

posted @   SuperTao1024  阅读(212)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示