bool WriteTxtFile(string filename, string content) { bool bRet = false; FileStream fs = null; StreamWriter sw = null; try { FileInfo fi = new FileInfo(filename); if (!fi.Directory.Exists) { Directory.CreateDirectory(fi.Directory.FullName); } //fs = new FileStream(filename, FileMode.Create, FileAccess.Write); fs = new FileStream(filename, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs, Encoding.Default); //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff:ffffff")) string s = string.Format("{0} {1} ", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"), content); sw.Write(s); sw.Flush(); bRet = true; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (sw != null) sw.Close(); if (fs != null) fs.Close(); } return bRet; }