码迷,mamicode.com
首页 > Windows程序 > 详细

C# 读写txt

时间:2017-12-04 13:26:18      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:stream   def   write   set   turn   object   datetime   读写   path   

//写入txt
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    LogCont log = new LogCont
                    {
                        LogId = LogId,
                        CurTime = DateTime.Now
                    };
                    byte[] LogContent = Encoding.Default.GetBytes(JsonConvert.SerializeObject(log));
                    fs.SetLength(0);
                    fs.Write(LogContent, 0, LogContent.Length);
                    fs.Flush();
                }

//读取txt
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        fs.Seek(0, SeekOrigin.Begin);
                        var btlength = fs.Length;
                        byte[] bt = new byte[btlength];
                        int r = fs.Read(bt, 0, bt.Length);
                        string ret = Encoding.Default.GetString(bt);

                        LogCont fRet = JsonConvert.DeserializeObject<LogCont>(ret);

                        return fRet;
                    }

 

C# 读写txt

标签:stream   def   write   set   turn   object   datetime   读写   path   

原文地址:http://www.cnblogs.com/DONET-LC/p/7976439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!