PybyCheckController.cs 4.6 KB

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