ReSampleHelper.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace CheckServer
  9. {
  10. /// <summary>
  11. /// 变采样帮助类
  12. /// </summary>
  13. public static class ReSampleHelper
  14. {
  15. private static string exePath = "AddIns";
  16. private const string exeName = "ReSample.exe";
  17. /// <summary>
  18. /// 设置【ReSample.exe】文件所在路径,支持相对路径
  19. /// </summary>
  20. public static void SetExePath(string path)
  21. {
  22. if (string.IsNullOrWhiteSpace(path)) return;
  23. if (path.StartsWith("\\"))//相对路径要么开头不带\,要么是 .\
  24. path = path.Remove(0, 1);
  25. exePath = path;
  26. }
  27. /// <summary>
  28. /// 变采样
  29. /// </summary>
  30. /// <param name="inFile">输入文件</param>
  31. /// <param name="outFile">输出文件</param>
  32. /// <param name="insertFactor">插入因子</param>
  33. /// <param name="extFactor">抽取因子</param>
  34. /// <returns>成功后返回文件,失败后返回null</returns>
  35. public static string Resample(string inFile, string outFile, int insertFactor, int extFactor)
  36. {
  37. if (string.IsNullOrWhiteSpace(exePath))
  38. throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");
  39. if (!Directory.Exists(exePath))
  40. throw new Exception($"路径[{exePath}]不存在");
  41. var exeFile = Path.Combine(exePath, exeName);
  42. if (!File.Exists(exeFile))
  43. throw new Exception($"文件[{exeFile}]不存在");
  44. Process p = new Process();
  45. p.StartInfo.WorkingDirectory = exePath;
  46. p.StartInfo.FileName = exeFile;
  47. p.StartInfo.Arguments = $"\"{inFile}\" \"{outFile}\" {insertFactor} {extFactor}";
  48. p.StartInfo.CreateNoWindow = true;
  49. p.StartInfo.RedirectStandardError = true;
  50. p.StartInfo.RedirectStandardOutput = true;
  51. p.StartInfo.UseShellExecute = false;
  52. p.Start();
  53. var succeed = p.WaitForExit(60 * 1000);
  54. if (!succeed)
  55. {
  56. throw new Exception($"进程[{exeName}]超时(60秒)未完成!");
  57. }
  58. var res = p.StandardOutput.ReadToEnd();
  59. if (res.StartsWith("1:"))
  60. {
  61. return outFile;
  62. }
  63. else
  64. {
  65. return null;
  66. }
  67. }
  68. }
  69. }