ReadFile.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace ReadFile
  7. {
  8. public class ReadFile
  9. {
  10. public string ReadFileList(string inParams)
  11. {
  12. string errorCode = "0";
  13. string errorMessage = "";
  14. JObject jObject = new JObject();
  15. JArray fileArray = new JArray();
  16. try
  17. {
  18. JObject inputEmpParamsObj = JObject.Parse(inParams);
  19. var paramsdata = inputEmpParamsObj["params"][0];
  20. string filePath = paramsdata["filePath"].ToString().Replace("/", "\\"); //文档路径
  21. DirectoryInfo di = new DirectoryInfo(filePath.TrimEnd('\\'));
  22. FileInfo[] files = new string[] { "*.*" }.SelectMany(i => di.GetFiles(i, SearchOption.AllDirectories)).Distinct().ToArray();
  23. Array.Sort(files, delegate (FileInfo x, FileInfo y) { return y.CreationTime.CompareTo(x.CreationTime); });
  24. for (int i = 0; i < files.Length; i++)
  25. {
  26. string xmldate = "";
  27. using (var sr = new StreamReader(files[i].FullName))
  28. {
  29. // Read the stream as a string, and write the string to the console.
  30. xmldate = sr.ReadToEnd();
  31. }
  32. JObject listobj = new JObject();
  33. listobj.Add("creationTime", files[i].CreationTime);
  34. listobj.Add("filesname", files[i].Name);
  35. listobj.Add("filePath", files[i].FullName.Replace("\\", "/"));
  36. listobj.Add("xml", xmldate);
  37. fileArray.Add(listobj);
  38. }
  39. }
  40. catch (Exception ex2)
  41. {
  42. errorCode = "-1";
  43. errorMessage = ex2.Message;
  44. }
  45. jObject.Add("errorCode", errorCode);
  46. jObject.Add("errorMessage", errorMessage);
  47. jObject.Add("result", fileArray);
  48. return jObject.ToString();
  49. }
  50. /// <summary>
  51. /// 复制文件
  52. /// </summary>
  53. /// <param name="sources">源路径</param>
  54. /// <param name="dest">新路径</param>
  55. public string FileMove(string inParams)
  56. {
  57. string errorCode = "0";
  58. string errorMessage = "";
  59. JObject jObject = new JObject();
  60. try
  61. {
  62. JObject inputEmpParamsObj = JObject.Parse(inParams);
  63. var paramsdata = inputEmpParamsObj["params"][0];
  64. string filePath = paramsdata["filePath"].ToString().Replace("/", "\\"); //文档路径
  65. String fileName = System.IO.Path.GetFileName(filePath); //'获取文件名
  66. string fileToPath = paramsdata["fileToPath"].ToString().Replace("/", "\\") + "\\"+ fileName; //文档路径
  67. string curDir = "";
  68. string fullDir = FtpParseDirectory(paramsdata["fileToPath"].ToString()+"/");
  69. string[] dirs = fullDir.Split('/');
  70. for (int i = 0; i < dirs.Length; i++)
  71. {
  72. string dir = dirs[i];
  73. //如果是以/开始的路径,第一个为空
  74. if (dir != null && dir.Length > 0)
  75. {
  76. curDir += dir + "/";
  77. if (Directory.Exists(curDir))
  78. {
  79. //已存在
  80. }
  81. else {
  82. //不存在创建
  83. Directory.CreateDirectory(curDir);
  84. }
  85. }
  86. }
  87. File.Move(filePath, fileToPath);
  88. }
  89. catch (Exception ex2)
  90. {
  91. errorCode = "-1";
  92. errorMessage = ex2.Message;
  93. }
  94. jObject.Add("errorCode", errorCode);
  95. jObject.Add("errorMessage", errorMessage);
  96. return jObject.ToString();
  97. }
  98. public static string FtpParseDirectory(string destFilePath)
  99. {
  100. return destFilePath.Substring(0, destFilePath.LastIndexOf("/"));
  101. }
  102. }
  103. }