using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace ReadFile { public class ReadFile { public string ReadFileList(string inParams) { string errorCode = "0"; string errorMessage = ""; JObject jObject = new JObject(); JArray fileArray = new JArray(); try { JObject inputEmpParamsObj = JObject.Parse(inParams); var paramsdata = inputEmpParamsObj["params"][0]; string filePath = paramsdata["filePath"].ToString().Replace("/", "\\"); //文档路径 DirectoryInfo di = new DirectoryInfo(filePath.TrimEnd('\\')); FileInfo[] files = new string[] { "*.*" }.SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories)).Distinct().ToArray(); Array.Sort(files, delegate (FileInfo x, FileInfo y) { return y.CreationTime.CompareTo(x.CreationTime); }); for (int i = 0; i < files.Length; i++) { string xmldate = ""; using (var sr = new StreamReader(files[i].FullName)) { // Read the stream as a string, and write the string to the console. xmldate = sr.ReadToEnd(); } JObject listobj = new JObject(); listobj.Add("creationTime", files[i].CreationTime); listobj.Add("filesname", files[i].Name); listobj.Add("filePath", files[i].FullName.Replace("\\", "/")); listobj.Add("xml", xmldate); fileArray.Add(listobj); } } catch (Exception ex2) { errorCode = "-1"; errorMessage = ex2.Message; } jObject.Add("errorCode", errorCode); jObject.Add("errorMessage", errorMessage); jObject.Add("result", fileArray); return jObject.ToString(); } /// /// 复制文件 /// /// 源路径 /// 新路径 public string FileMove(string inParams) { string errorCode = "0"; string errorMessage = ""; JObject jObject = new JObject(); try { JObject inputEmpParamsObj = JObject.Parse(inParams); var paramsdata = inputEmpParamsObj["params"][0]; string filePath = paramsdata["filePath"].ToString().Replace("/", "\\"); //文档路径 String fileName = System.IO.Path.GetFileName(filePath); //'获取文件名 string fileToPath = paramsdata["fileToPath"].ToString().Replace("/", "\\") + "\\"+ fileName; //文档路径 string curDir = ""; string fullDir = FtpParseDirectory(paramsdata["fileToPath"].ToString()+"/"); string[] dirs = fullDir.Split('/'); for (int i = 0; i < dirs.Length; i++) { string dir = dirs[i]; //如果是以/开始的路径,第一个为空 if (dir != null && dir.Length > 0) { curDir += dir + "/"; if (Directory.Exists(curDir)) { //已存在 } else { //不存在创建 Directory.CreateDirectory(curDir); } } } File.Move(filePath, fileToPath); } catch (Exception ex2) { errorCode = "-1"; errorMessage = ex2.Message; } jObject.Add("errorCode", errorCode); jObject.Add("errorMessage", errorMessage); return jObject.ToString(); } public static string FtpParseDirectory(string destFilePath) { return destFilePath.Substring(0, destFilePath.LastIndexOf("/")); } } }