C#向文件中追加字符

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using System.IO;
namespace OpeartorFile
{
    /// <summary>
    /// 范小军
    /// 用来学习向文件中追加字符的方法
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {

            FileInfo fi = new FileInfo(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"SBILog.txt");//创建一个文件信息
            if (fi.Exists && fi.Length > 1024 * 1024)
            {//先判断是否存在改名称的文件,并且判断文件的大小
                fi.Delete();
            }
            FileStream fs = new FileStream(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"SBILog.txt", FileMode.OpenOrCreate, FileAccess.Write);//创建一个文件流
            StreamWriter m_streamWriter = new StreamWriter(fs);//创建一个文件输入流
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);//字符追加的位置
            m_streamWriter.WriteLine("Msg:" + DateTime.Now.ToString() + " -- " + System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\n");//向文件中写入字符串
            m_streamWriter.Flush();//清除此流的缓冲区,使得所有缓冲的数据都写入到文件中。
            m_streamWriter.Close();//关闭输入流
            fs.Close();//关闭文件流
        }
    }
}
原文地址:https://www.cnblogs.com/fanxiaojun/p/2428022.html