PybyCheckClass.cs 4.5 KB

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