tshm
6/13/2015 - 7:51 AM

command execution example

command execution example


        private int runCommand(string cmd, string args)
        {
            Log("コマンド実行:\n\t{0} {1}\n", cmd, args);
            System.Diagnostics.Process process = new System.Diagnostics.Process()
            {
                StartInfo = new System.Diagnostics.ProcessStartInfo()
                {
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    FileName = cmd,
                    Arguments = args
                    //WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
                }
            };
            process.OutputDataReceived += (sender, arg) => Log(">> " + arg.Data + "\n");
            process.Start();
            process.BeginOutputReadLine();
            process.WaitForExit(1000 * settings.CmdTimeoutSecs);
            var code = process.ExitCode;
            process.Close();
            Log("exit code: {0}\n", code);
            return code;
        }