1. void Awake()
    2. {
    3. //检测程序是否关闭 2018版本以上的
    4. Application.wantsToQuit += () =>
    5. {
    6. string consoleLogPath = Application.consoleLogPath; //日志路径
    7. if (File.Exists(consoleLogPath))
    8. {
    9. string filePath = Path.GetDirectoryName(consoleLogPath);
    10. string fileExtension = Path.GetExtension(consoleLogPath);
    11. string fileName = Path.GetFileNameWithoutExtension(consoleLogPath);
    12. string OutLogFilePath = Application.dataPath + "/../LogOut/" + fileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + fileExtension;
    13. string NewfilePath = Path.GetDirectoryName(OutLogFilePath);
    14. if (Directory.Exists(NewfilePath) == false)
    15. {
    16. Directory.CreateDirectory(NewfilePath);
    17. }
    18. File.Copy(consoleLogPath, OutLogFilePath, true);
    19. //当文件满足六个时,保留时间最新的三个文件
    20. DirectoryInfo dir = new DirectoryInfo(NewfilePath);
    21. if (dir.GetFiles().Length > 5)
    22. {
    23. string logFileType = "*.log";
    24. string[] filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);
    25. Dictionary<string, DateTime> fileCreateDate = new Dictionary<string, DateTime>();
    26. if (filePathArr.Length == 0)
    27. {
    28. logFileType = "*.txt";
    29. filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);
    30. }
    31. for (int i = 0; i < filePathArr.Length; i++)
    32. {
    33. FileInfo fi = new FileInfo(filePathArr[i]);
    34. fileCreateDate[filePathArr[i]] = fi.CreationTime;
    35. }
    36. fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
    37. foreach (KeyValuePair<string, DateTime> item in fileCreateDate)
    38. {
    39. if (File.Exists(item.Key) && Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly).Length > 3)
    40. {
    41. File.Delete(item.Key);
    42. }
    43. }
    44. }
    45. }
    46. return true;
    47. };
    48. }
    49. /// <summary>
    50. /// 程序关闭(正常)2017版本以下的(不包括右键关闭exe程序)
    51. /// </summary>
    52. void OnApplicationQuit()
    53. {
    54. string consoleLogPath = Application.consoleLogPath; //日志路径
    55. if (File.Exists(consoleLogPath))
    56. {
    57. string filePath = Path.GetDirectoryName(consoleLogPath);
    58. string fileExtension = Path.GetExtension(consoleLogPath);
    59. string fileName = Path.GetFileNameWithoutExtension(consoleLogPath);
    60. string OutLogFilePath = Application.dataPath + "/../LogOut/" + fileName + "_" + DateTime.Now.ToString("yyyyMMddHHmm") + fileExtension;
    61. string NewfilePath = Path.GetDirectoryName(OutLogFilePath);
    62. if (Directory.Exists(NewfilePath) == false)
    63. {
    64. Directory.CreateDirectory(NewfilePath);
    65. }
    66. File.Copy(consoleLogPath, OutLogFilePath, true);
    67. //当文件满足六个时,保留时间最新的三个文件
    68. DirectoryInfo dir = new DirectoryInfo(NewfilePath);
    69. if (dir.GetFiles().Length > 5)
    70. {
    71. string logFileType = "*.log";
    72. string[] filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);
    73. Dictionary<string, DateTime> fileCreateDate = new Dictionary<string, DateTime>();
    74. if (filePathArr.Length == 0)
    75. {
    76. logFileType = "*.txt";
    77. filePathArr = Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly);
    78. }
    79. for (int i = 0; i < filePathArr.Length; i++)
    80. {
    81. FileInfo fi = new FileInfo(filePathArr[i]);
    82. fileCreateDate[filePathArr[i]] = fi.CreationTime;
    83. }
    84. fileCreateDate = fileCreateDate.OrderBy(f => f.Value).ToDictionary(f => f.Key, f => f.Value);
    85. foreach (KeyValuePair<string, DateTime> item in fileCreateDate)
    86. {
    87. if (File.Exists(item.Key) && Directory.GetFiles(NewfilePath, logFileType, SearchOption.TopDirectoryOnly).Length > 3)
    88. {
    89. File.Delete(item.Key);
    90. }
    91. }
    92. }
    93. }
    94. }