把内容生成txt文件

时间:2021-12-16 20:51:50

StringBuilder MailLog = new StringBuilder();

            string logPath = txtFile + str + DateTime.Now.ToString("yyyyMMdd") + ".txt";

            Write(logPath, Context.DateFormat + " - " + message);

public static void Write(string filePath, string message, bool isNewline = true, Encoding encoding = null, bool append = true)
        {
            encoding = encoding ?? SlEncoding.Default;

            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(filePath));
            }
            catch { }

            using (var streamWriter = new StreamWriter(filePath, append, encoding))
            {
                if (isNewline)
                {
                    streamWriter.WriteLine(message);
                }
                else
                {
                    streamWriter.Write(message);
                }
                streamWriter.Flush();
            }
        }