Exemple #1
0
        /// <summary>
        /// 获取执行CMD命令后需裁剪的部分
        /// </summary>
        /// <param name="cmd">CMD命令</param>
        /// <returns></returns>
        public static List <string> GetTailors(string cmd, string splitKeyword = @"\s*(\r\n)+\s*")
        {
            ProcessStartInfo psi = new ProcessStartInfo("CMD");

            psi.UseShellExecute        = false;
            psi.RedirectStandardInput  = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;
            psi.CreateNoWindow         = true;

            Process proc = new Process();

            proc.StartInfo = psi;
            proc.Start();
            proc.StandardInput.WriteLine(cmd + " >nul 2>nul");
            proc.StandardInput.WriteLine("exit");
            proc.WaitForExit();
            string echo = proc.StandardOutput.ReadToEnd();

            echo = echo.Replace(" >nul 2>nul", string.Empty);

            Regex splitKWRegex = new Regex(splitKeyword);
            Regex ignoreRegex  = new Regex("^" + splitKeyword + "$");

            string[]      origTailors = splitKWRegex.Split(echo);
            List <string> tailors     = Lst.Grep <string>(new List <string>(origTailors), (origVal) => {
                string val = origVal.ToString();
                return(!Str.IsNullOrWhiteSpace(val) && !ignoreRegex.IsMatch(val));
            });

            return(tailors);
        }