| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 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();
- }
- /// <summary>
- /// 复制文件
- /// </summary>
- /// <param name="sources">源路径</param>
- /// <param name="dest">新路径</param>
- 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("/"));
- }
- }
- }
|