Log.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. namespace prBrowser
  6. {
  7. public class Log
  8. {
  9. public static void LogInfo(string logInput, string directory)
  10. {
  11. string spcfolder = Application.StartupPath;
  12. spcfolder = spcfolder.Replace("\\", "/");
  13. string nowData = DateTime.Now.ToString("yyyy-MM-dd");
  14. nowData = nowData.Replace("-", null);
  15. //QY 搜索文件路径中是否存在所需文件夹,不存在则创建
  16. if (!string.IsNullOrWhiteSpace(spcfolder))
  17. {
  18. if (!Directory.Exists(spcfolder + "/service/"))
  19. {
  20. //Directory.CreateDirectory(spcfolder + "/service/");
  21. Directory.CreateDirectory(spcfolder + "/service/log");
  22. }
  23. else if (!Directory.Exists(spcfolder + "/service/log/"))
  24. {
  25. Directory.CreateDirectory(spcfolder + "/service/log");
  26. }
  27. }
  28. string inputDir = spcfolder + "/service/log/" + directory + nowData + ".txt";
  29. FileStream fileStreamObj = new FileStream(inputDir, FileMode.Append, FileAccess.Write);
  30. string localDateTime = DateTime.Now.ToLocalTime().ToString();
  31. logInput = localDateTime + ": " + logInput + "\r\n";
  32. byte[] data = Encoding.Default.GetBytes(logInput);
  33. fileStreamObj.Write(data, 0, data.Length);
  34. fileStreamObj.Flush();
  35. fileStreamObj.Close();
  36. }
  37. }
  38. }