c# 写入文件

 if (!File.Exists(path))
                {
                    File.Create(path).Close();
                }
                using (StreamWriter w = File.AppendText(path))
                {
                    w.WriteLine("Error log:");
                    w.WriteLine("Error Time:"+DateTime.Now);
                    w.WriteLine("Error TargetSite:" + error.TargetSite);
                    w.WriteLine("Error Message:" + error.Message);
                    w.WriteLine("Error StackTrace" + error.StackTrace);
                    w.WriteLine("-----------------------------------------------------------------------------------------------------------------------");
                    w.Flush();
                    w.Close();
                }
原文地址:https://www.cnblogs.com/liuxinls/p/3075957.html