| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 | using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CheckServer{    /// <summary>    /// 变采样帮助类    /// </summary>    public static class ReSampleHelper    {        private static string exePath = "AddIns";        private const string exeName = "ReSample.exe";        /// <summary>        /// 设置【ReSample.exe】文件所在路径,支持相对路径        /// </summary>        public static void SetExePath(string path)        {            if (string.IsNullOrWhiteSpace(path)) return;            if (path.StartsWith("\\"))//相对路径要么开头不带\,要么是 .\                path = path.Remove(0, 1);            exePath = path;        }        /// <summary>        /// 变采样        /// </summary>        /// <param name="inFile">输入文件</param>        /// <param name="outFile">输出文件</param>        /// <param name="insertFactor">插入因子</param>        /// <param name="extFactor">抽取因子</param>        /// <param name="timeoutSeconds">超时时间</param>        /// <returns>成功后返回文件,失败后返回null</returns>        public static string Resample(string inFile, string outFile, int insertFactor, int extFactor, int timeoutSeconds = 30)        {            if (string.IsNullOrWhiteSpace(exePath))                throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");            if (!Directory.Exists(exePath))                throw new Exception($"路径[{exePath}]不存在");            var exeFile = Path.Combine(exePath, exeName);            if (!File.Exists(exeFile))                throw new Exception($"文件[{exeFile}]不存在");            Process p = new Process();            p.StartInfo.WorkingDirectory = exePath;            p.StartInfo.FileName = exeFile;            p.StartInfo.Arguments = $"\"{inFile}\" \"{outFile}\" {insertFactor} {extFactor}";            p.StartInfo.CreateNoWindow = true;            p.StartInfo.RedirectStandardError = true;            p.StartInfo.RedirectStandardOutput = true;            p.StartInfo.UseShellExecute = false;            p.Start();            var succeed = p.WaitForExit(timeoutSeconds * 1000);            if (!succeed)            {                throw new Exception($"进程[{exeName}]超时未完成!");            }            var res = p.StandardOutput.ReadToEnd();            if (res.StartsWith("1:"))            {                return outFile;            }            else            {                return null;            }        }    }}
 |