как справиться с вопросом да или нет при запуске процесса - PullRequest
0 голосов
/ 04 марта 2019

Я пытаюсь выполнить команду 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 Метка -prI: \ PVCS \ DEVELOPMENT\ -idabcd: abcpass -v'test3 '-f -z' /Project1/APPLICATION/ajax_fetchGetCustNew.php 'Неизвестно os = Windows NT (неизвестно) Менеджер версий Serena PVCS (PCLI) v8.4.0.0 (сборка 668) для WindowsNT / 80x86 Copyright 1985-2010 Serena Software.Все права защищены.Версия "test3" уже определена в архиве "I: \ PVCS \ DEVELOPMENT \ archives \ Project1 \ Application \ ajax_fetchGetCustNew.php-arc".Переписать?(да / нет)

1 Ответ

0 голосов
/ 04 марта 2019
using(var myProcess = new Process()) {
    ...

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

    ...
}

Примечание. Этот подход не очень пригоден для повторного использования.

Использование параметра командной строки, например /no-confirm (как Джон Ву предложил в комментариях к вопросам), предпочтительнее, если он существует.

...