string date = DateTime.Now.ToString("yyyy-MM-dd"); string rootPath = System.Environment.CurrentDirectory; //string folderPath = rootPath + @"Logs"; string folderPath = @"D:\Logs"; string logPath = folderPath + "\" + date + @".txt"; if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } if (!File.Exists(logPath)) { File.Create(logPath); } FileStream fileStream = new FileStream(logPath, FileMode.Append); StreamWriter writer = new StreamWriter(fileStream); string currentTime = DateTime.Now.ToString(); writer.WriteLine("【" + currentTime + "】:" + str); writer.Dispose(); writer.Close(); fileStream.Dispose(); fileStream.Close();