C#寫入文件

1.採用打開創建的方式,同名寫入,不同名創建 ,最近接手一個項目,一些好用的日誌組件領導不也不用,自己寫唄

       void WriteLogFile(string Content)
        {           
           
                string strFoloder = AppDomain.CurrentDomain.BaseDirectory + "\\applogs";
                if (!Directory.Exists(strFoloder))
                {
                    Directory.CreateDirectory(strFoloder);
                }
                string strFileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";
                FileStream fs = new FileStream(strFoloder + "\\" + strFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine(Content);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章