如何在 运行 个过程中处理是或否问题

how to handle yes or no question while running a process

我正在尝试通过 C# 中的进程执行命令,但问题是该命令提出问题 (y/n) 并且进程挂在那里。你介意给我推荐一个解决方案吗?

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;
        }

我的命令输出如下:

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)

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

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

    ...
}

注意:此方法的可重用性不高。

最好使用像 /no-confirm 这样的命令行选项(如 John Wu 在问题评论中所建议的那样),如果存在的话。