天天看點

C#清空txt内容,并追加内容

1、将txt内容清空

public void ClearTxt(String txtPath)
        {
            String appDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Txt\" + txtPath;
            FileStream stream = File.Open(appDir, FileMode.OpenOrCreate, FileAccess.Write);
            stream.Seek(0, SeekOrigin.Begin);
            stream.SetLength(0);
            stream.Close();
        }
           

2、向txt中追加内容

public void SaveFile(string str,String txtPath,bool saOrAp) {

            String appDir = System.AppDomain.CurrentDomain.BaseDirectory + @"Txt\" + txtPath;
            StreamWriter sw = new StreamWriter(appDir, saOrAp);//saOrAp表示覆寫或者是追加
            sw.WriteLine(str);
            sw.Close();
        }