using BaiduYunBeiFen.ResultModel;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BaiduYunBeiFen.Controller
{
public class PybyCheckClass
{
///
/// 查询云路径下的文件列表
///
///
///
public List CheckPathFile(string YunPath)
{
//存储解析后的全部信息
List Result = new List();
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;
}
///
/// 去除队列中的警告信息及已知的提示信息
///
/// 需要删选的队列
/// 返回被检查的云端文件地址
///
private List ReMoveWarnMessage(List MessageList, out string YunPath)
{
List Result = new();
List MessageCopy = new List(MessageList);
List WarnList = new List()
{
"",
"Files with non-ASCII names may not be handled correctly.",
"You should set your System Locale to 'UTF-8'.",
"Current locale is 'cp65001'"
};
foreach (string Message in MessageList)
{
if (WarnList.Exists(t => Message.Contains(t)))
{
MessageCopy.Remove(Message);
}
}
YunPath = MessageCopy[0].Split(' ')[0];
MessageCopy.Remove(MessageCopy[0]);
Result = PareseMessage(MessageCopy);
return Result;
}
///
/// 将文件列表数据由字符串转成类
///
///
///
private List PareseMessage(List FileList)
{
List Result = new();
FileList.ForEach(File => {
string[] strGetAllMessage = File.Split(' ');
if(strGetAllMessage.Count() == 6)
{
try
{
string[] FileNameSplit = strGetAllMessage[1].Split(".");
string GetFileType = strGetAllMessage[0] == "D" ? "Folder" : FileNameSplit[FileNameSplit.Count() - 1];
int GetFileSize = 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;
}
}
}