0

I am trying to execute a command by Process in c#, but problem is that that command ask a question (y/n) and process hang there. would you mind recommend me a solution?

public static OutputEventArgs execSync(string exe, string arguments)
        {
            OutputEventArgs oea = new OutputEventArgs();
            try
            {
                using (Process myProcess = new Process())
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.RedirectStandardError = true;
                    startInfo.UseShellExecute = false;
                    startInfo.CreateNoWindow = true;

                    startInfo.FileName = exe;
                    startInfo.Arguments = arguments;
                    myProcess.StartInfo = startInfo;
                    myProcess.Start();
                    oea.Data = myProcess.StandardOutput.ReadToEnd();
                    myProcess.WaitForExit();
                    oea.exitCode = myProcess.ExitCode;
                }
            }catch(Exception e)
            {
                oea.Data = e.Message;
                oea.ExceptionHappened();
            }
            return oea;
        }

my command output is something like below:

C:\Users\abc>pcli Label -prI:\PVCS\DEVELOPMENT\ -idabcd:abcpass -v'test3' -f -z '/Project1/APPLICATION/ajax_fetchGetCustNew.php' Unknown os = Windows NT (unknown) Serena PVCS Version Manager (PCLI) v8.4.0.0 (Build 668) for Windows NT/80x86 Copyright 1985-2010 Serena Software. All rights reserved. Version "test3" is already defined in archive "I:\PVCS\DEVELOPMENT\archives\Project1\Application\ajax_fetchGetCustNew.php-arc". Overwrite? (y/n)

4

1 回答 1

1
using(var myProcess = new Process()) {
    ...

    myProcess.Start();
    myProcess.StandardInput.WriteLine("y"); // Write 'y' to the processes' console input

    ...
}

注意:这种方法不是很可重用。

如果存在命令行选项/no-confirm(如 John Wu 在问题评论中建议的那样),则更可取。

于 2019-03-04T12:22:25.950 回答