C# 读写txt

2021-04-24 06:28

阅读:429

标签: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(ret);

                        return fRet;
                    }

 

C# 读写txt

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

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

上一篇:wget window

下一篇:C#常见函数


评论


亲,登录后才可以留言!