Logs.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. namespace PaintDotNet.Camera
  5. {
  6. public class Logs
  7. {
  8. private static string m_filePath = Application.StartupPath + "\\CameraLogs\\";
  9. private static void chkFile()
  10. {
  11. if (!Directory.Exists(m_filePath))
  12. {
  13. Directory.CreateDirectory(m_filePath); //不存在就创建目录
  14. }
  15. }
  16. public static void Write(string str)
  17. {
  18. try
  19. {
  20. #if DEBUG
  21. str = DateTime.Now.ToString("HH:mm:ss:fff") + ":" + str;
  22. chkFile();
  23. StreamWriter sw = File.AppendText(m_filePath + DateTime.Now.ToString("log") + ".txt");
  24. //开始写入
  25. sw.WriteLine(str);
  26. //清空缓冲区
  27. sw.Flush();
  28. //关闭流
  29. sw.Close();
  30. #endif
  31. }
  32. catch (Exception)
  33. {
  34. }
  35. }
  36. public static void Clear()
  37. {
  38. try
  39. {
  40. File.Delete(m_filePath + DateTime.Now.ToString("log") + ".txt");
  41. }
  42. catch (Exception) { }
  43. }
  44. }
  45. }