RemoteLoader.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Reflection;
  3. using System.Windows.Forms;
  4. using Newtonsoft.Json.Linq;
  5. namespace prBrowser
  6. {
  7. public class RemoteLoader : MarshalByRefObject
  8. {
  9. public string GetValue(string input)
  10. {
  11. //System.Diagnostics.Debugger.Launch();
  12. JObject jObject = new JObject();
  13. string retstring = "";
  14. string directory = "";
  15. try
  16. {
  17. JObject jsonObj = JObject.Parse(input);
  18. string inputDir = "";
  19. string clsName = "";
  20. string funcName = "";
  21. string dllInput = "";
  22. if (jsonObj["inputDir"] != null)
  23. {
  24. inputDir = jsonObj["inputDir"]!.ToString();
  25. directory = inputDir.Replace("/", ".").Replace("\\", ".");
  26. string spcfolder = Application.StartupPath;
  27. spcfolder = spcfolder.Replace("\\", "/");
  28. //inputDir = spcfolder + "/service/plugins/" + inputDir;
  29. //QY 重写GetIP.dll文件路径
  30. if (inputDir.IndexOf("GetIP.dll") !=-1)
  31. {
  32. inputDir = "/GetIP.dll";
  33. }
  34. inputDir = spcfolder + inputDir;
  35. }
  36. if (jsonObj["clsName"] != null)
  37. {
  38. clsName = jsonObj["clsName"]!.ToString();
  39. }
  40. if (jsonObj["funcName"] != null)
  41. {
  42. funcName = jsonObj["funcName"]!.ToString();
  43. }
  44. if (jsonObj["dllInput"] != null)
  45. {
  46. dllInput = jsonObj["dllInput"]!.ToString();
  47. }
  48. object[] temParts = null;
  49. if (dllInput != "")
  50. {
  51. temParts = new object[1] { dllInput };
  52. }
  53. string path = inputDir;
  54. Assembly assem = Assembly.LoadFrom(path);
  55. Type clsType = assem.GetType(clsName);
  56. MethodInfo pMtInfo = clsType.GetMethod(funcName);
  57. object tObj = Activator.CreateInstance(clsType);
  58. retstring = (string)pMtInfo.Invoke(tObj, temParts);
  59. }
  60. catch (Exception ex)
  61. {
  62. jObject.Add("errorCode", (JToken)"-1");
  63. string Messageerr = ex.Message;
  64. if (ex.InnerException != null)
  65. { Messageerr = ex.InnerException.Message; }
  66. jObject.Add("errorMessage", (JToken)Messageerr);
  67. retstring = jObject.ToString();
  68. }
  69. return retstring;
  70. }
  71. /// <summary>
  72. ///QY 复制文件夹及文件到指定目录
  73. /// </summary>
  74. /// <param name="srcPath"></param>
  75. /// <param name="aimPath"></param>
  76. private void CopyDir(string srcPath, string aimPath)
  77. {
  78. try
  79. {
  80. // 检查目标目录是否以目录分割字符结束如果不是则添加
  81. if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
  82. {
  83. aimPath += System.IO.Path.DirectorySeparatorChar;
  84. }
  85. // 判断目标目录是否存在如果不存在则新建
  86. if (!System.IO.Directory.Exists(aimPath))
  87. {
  88. System.IO.Directory.CreateDirectory(aimPath);
  89. }
  90. // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
  91. // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
  92. // string[] fileList = Directory.GetFiles(srcPath);
  93. string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);
  94. // 遍历所有的文件和目录
  95. foreach (string file in fileList)
  96. {
  97. // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
  98. if (System.IO.Directory.Exists(file))
  99. {
  100. CopyDir(file, aimPath + System.IO.Path.GetFileName(file));
  101. }
  102. // 否则直接Copy文件
  103. else
  104. {
  105. System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true);
  106. }
  107. }
  108. }
  109. catch (Exception e)
  110. {
  111. throw;
  112. }
  113. }
  114. }
  115. }