123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- using BaiduYunBeiFen.ResultModel;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Reflection.Metadata.Ecma335;
- using System.Text;
- using System.Threading.Tasks;
- namespace BaiduYunBeiFen.Controller
- {
- public class PybyCheckController
- {
- /// <summary>
- /// 查询云路径下的文件列表
- /// </summary>
- /// <param name="YunPath"></param>
- /// <returns></returns>
- public List<FileMessageModelClass> CheckPathFile(string YunPath)
- {
- //存储解析后的全部信息
- List<FileMessageModelClass> Result = new List<FileMessageModelClass>();
- string OutYunPath;
- //新建CMD程序
- ProcessStartInfo processStartInfo = new ProcessStartInfo();
- processStartInfo.FileName = "cmd.exe";
- processStartInfo.Arguments = YunPath;
- processStartInfo.RedirectStandardOutput = true;
- processStartInfo.UseShellExecute = false;
- processStartInfo.CreateNoWindow = true;
- // 创建并启动Process对象
- Process process = new Process();
- process.StartInfo = processStartInfo;
- process.Start();
- // 读取CMD输出
- string output = process.StandardOutput.ReadToEnd();
- //退出ProCess
- process.WaitForExit();
- process.Dispose();
- //把读取到的输出数据按行划分
- string[] Message = output.Split(Environment.NewLine);
- //去除警告信息
- Result = ReMoveWarnMessage(Message.ToList(), out OutYunPath);
- return Result;
- }
- /// <summary>
- /// 去除队列中的警告信息及已知的提示信息
- /// </summary>
- /// <param name="FileList">需要删选的队列</param>
- /// <param name="YunPath">返回被检查的云端文件地址</param>
- /// <returns></returns>
- private List<FileMessageModelClass> ReMoveWarnMessage(List<string> MessageList, out string YunPath)
- {
- List<FileMessageModelClass> Result = new();
- List<string> MessageCopy = new List<string>(MessageList);
- List<string> WarnList = new List<string>()
- {
- "<W>",
- "Files with non-ASCII names may not be handled correctly.",
- "You should set your System Locale to 'UTF-8'.",
- "Current locale is",
- };
- foreach (string Message in MessageList)
- {
- if (WarnList.Exists(t => Message.Contains(t))||Message.Length<=5)
- {
- MessageCopy.Remove(Message);
- }
- }
- YunPath = MessageCopy[0].Split(' ')[0];
- MessageCopy.Remove(MessageCopy[0]);
- Result = PareseMessage(MessageCopy);
- return Result;
- }
- /// <summary>
- /// 将文件列表数据由字符串转成类
- /// </summary>
- /// <param name="FileList"></param>
- /// <returns></returns>
- private List<FileMessageModelClass> PareseMessage(List<string> FileList)
- {
- //用来存储结果的队列
- List<FileMessageModelClass> Result = new();
- //对传入的string队列进行解析
- FileList.ForEach(File => {
- string[] strGetAllMessage = File.Split(' ');
- //判定是否是文件信息string
- if(strGetAllMessage.Count() == 6)
- {
- try
- {
- //获取文件名后缀
- string[] FileNameSplit = strGetAllMessage[1].Split(".");
- string GetFileType = strGetAllMessage[0] == "D" ? "Folder" : FileNameSplit[FileNameSplit.Count() - 1];
- //获取文件大小
- int GetFileSize = strGetAllMessage[0] == "D" ? 0 : Convert.ToInt32(strGetAllMessage[2]);
- //获取文件改动时间
- DateTime GetFileTime = Convert.ToDateTime(strGetAllMessage[3] + strGetAllMessage[4]);
- //组合数据
- Result.Add(new FileMessageModelClass()
- {
- MessageType = strGetAllMessage[0],
- FileName = strGetAllMessage[1],
- FileType = GetFileType,
- FileSize = GetFileSize,
- FileYunChangeTime = GetFileTime,
- OtherCode = strGetAllMessage[5]
- });
- }
- catch
- {
- //报错
- Console.WriteLine("数据队列转换异常");
- FileList.ForEach(File => { Console.WriteLine(File); });
- }
- }
- });
- return Result;
- }
- }
- }
|