ReSampleHelper.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /// <param name="timeoutSeconds">超时时间</param>
  35. /// <returns>成功后返回文件,失败后返回null</returns>
  36. public static string Resample(string inFile, string outFile, int insertFactor, int extFactor, int timeoutSeconds = 30)
  37. {
  38. if (string.IsNullOrWhiteSpace(exePath))
  39. throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");
  40. if (!Directory.Exists(exePath))
  41. throw new Exception($"路径[{exePath}]不存在");
  42. var exeFile = Path.Combine(exePath, exeName);
  43. if (!File.Exists(exeFile))
  44. throw new Exception($"文件[{exeFile}]不存在");
  45. Process p = new Process();
  46. p.StartInfo.WorkingDirectory = exePath;
  47. p.StartInfo.FileName = exeFile;
  48. p.StartInfo.Arguments = $"\"{inFile}\" \"{outFile}\" {insertFactor} {extFactor}";
  49. p.StartInfo.CreateNoWindow = true;
  50. p.StartInfo.RedirectStandardError = true;
  51. p.StartInfo.RedirectStandardOutput = true;
  52. p.StartInfo.UseShellExecute = false;
  53. p.Start();
  54. var succeed = p.WaitForExit(timeoutSeconds * 1000);
  55. if (!succeed)
  56. {
  57. throw new Exception($"进程[{exeName}]超时未完成!");
  58. }
  59. var res = p.StandardOutput.ReadToEnd();
  60. if (res.StartsWith("1:"))
  61. {
  62. return outFile;
  63. }
  64. else
  65. {
  66. return null;
  67. }
  68. }
  69. }
  70. }