PybyClass.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using BaiduYunBeiFen.ResultModel;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace BaiduYunBeiFen.Controller
  10. {
  11. public class PybyClass
  12. {
  13. public List<FileMessageModelClass> CheckPathFile(string YunPath)
  14. {
  15. //存储解析后的全部信息
  16. List<FileMessageModelClass> Result = new List<FileMessageModelClass>();
  17. string OutYunPath;
  18. //新建CMD程序
  19. ProcessStartInfo processStartInfo = new ProcessStartInfo();
  20. processStartInfo.FileName = "cmd.exe";
  21. processStartInfo.Arguments = YunPath;
  22. processStartInfo.RedirectStandardOutput = true;
  23. processStartInfo.UseShellExecute = false;
  24. processStartInfo.CreateNoWindow = true;
  25. // 创建并启动Process对象
  26. Process process = new Process();
  27. process.StartInfo = processStartInfo;
  28. process.Start();
  29. // 读取CMD输出
  30. string output = process.StandardOutput.ReadToEnd();
  31. //退出ProCess
  32. process.WaitForExit();
  33. process.Dispose();
  34. //把读取到的输出数据按行划分
  35. string[] Message = output.Split(Environment.NewLine);
  36. //去除警告信息
  37. Result = ReMoveWarnMessage(Message.ToList(), out OutYunPath);
  38. return Result;
  39. }
  40. /// <summary>
  41. /// 去除队列中的警告信息及已知的提示信息
  42. /// </summary>
  43. /// <param name="FileList">需要删选的队列</param>
  44. /// <param name="YunPath">返回被检查的云端文件地址</param>
  45. /// <returns></returns>
  46. private List<FileMessageModelClass> ReMoveWarnMessage(List<string> MessageList, out string YunPath)
  47. {
  48. List<FileMessageModelClass> Result = new();
  49. List<string> MessageCopy = new List<string>(MessageList);
  50. List<string> WarnList = new List<string>()
  51. {
  52. "<W>",
  53. "Files with non-ASCII names may not be handled correctly.",
  54. "You should set your System Locale to 'UTF-8'.",
  55. "Current locale is 'cp65001'"
  56. };
  57. foreach (string Message in MessageList)
  58. {
  59. if (WarnList.Exists(t => Message.Contains(t)))
  60. {
  61. MessageCopy.Remove(Message);
  62. }
  63. }
  64. YunPath = MessageCopy[0].Split(' ')[0];
  65. MessageCopy.Remove(MessageCopy[0]);
  66. Result = PareseMessage(MessageCopy);
  67. return Result;
  68. }
  69. /// <summary>
  70. /// 将文件列表数据由字符串转成类
  71. /// </summary>
  72. /// <param name="FileList"></param>
  73. /// <returns></returns>
  74. private List<FileMessageModelClass> PareseMessage(List<string> FileList)
  75. {
  76. List<FileMessageModelClass> Result = new();
  77. FileList.ForEach(File => {
  78. string[] strGetAllMessage = File.Split(' ');
  79. if(strGetAllMessage.Count() == 6)
  80. {
  81. try
  82. {
  83. string[] FileNameSplit = strGetAllMessage[1].Split(".");
  84. string GetFileType = strGetAllMessage[0] == "D" ? "Folder" : FileNameSplit[FileNameSplit.Count() - 1];
  85. int GetOtherNum = Convert.ToInt32(strGetAllMessage[2]);
  86. DateTime GetFileTime = Convert.ToDateTime(strGetAllMessage[3] + strGetAllMessage[4]);
  87. Result.Add(new FileMessageModelClass()
  88. {
  89. MessageType = strGetAllMessage[0],
  90. FileName = strGetAllMessage[1],
  91. FileType = GetFileType,
  92. OtherNumber = GetOtherNum,
  93. FileYunChangeTime = GetFileTime,
  94. OtherCode = strGetAllMessage[5]
  95. });
  96. }
  97. catch
  98. {
  99. Console.WriteLine("数据队列转换异常");
  100. FileList.ForEach(File => { Console.WriteLine(File); });
  101. }
  102. }
  103. });
  104. return Result;
  105. }
  106. }
  107. }