Пример #1
0
        public Process GetKillProcess(string processName, CommandOptions options)
        {
            var startInfo = new ProcessStartInfo
            {
                CreateNoWindow         = false,
                WindowStyle            = ProcessWindowStyle.Normal,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                RedirectStandardError  = true
            };

            if (RuntimeInfo.IsWindows)
            {
                startInfo.FileName  = "taskkill";
                startInfo.Arguments = $"/F /IM {ProcessUtility.AddExtension(processName)}";
            }
            else
            {
                startInfo.FileName  = "killall";
                startInfo.Arguments = processName;
            }

            var process = new Process
            {
                StartInfo           = startInfo,
                EnableRaisingEvents = true,
            };

            process.OutputDataReceived += (o, e) =>
            {
                if (options.Verbose && !string.IsNullOrWhiteSpace(e.Data))
                {
                    _console.WriteLine(e.Data);
                }
            };
            process.ErrorDataReceived += (o, e) =>
            {
                if (!string.IsNullOrWhiteSpace(e.Data))
                {
                    _console.WriteWarningLine(e.Data);
                }
            };

            return(process);
        }