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)

Amir
  • 1,919
  • 8
  • 53
  • 105
  • Firstly there is nothing `async` about this code, id remove the `Async` suffix from the name – TheGeneral Mar 04 '19 at 07:02
  • ProcessWindowStyle.Hidden? How the question can be anwered when the window can't display it? – Daniel Dušek Mar 04 '19 at 07:11
  • @MichaelRandall: modified! – Amir Mar 04 '19 at 07:11
  • @dee: the system cannot handle it somehow when the question asked? it will be so ugly if I show the command prompt. – Amir Mar 04 '19 at 07:11
  • I don't know anything about PCLI but sometimes there are command line arguments that allow you to specify a default "overwrite behavior" - There seems to be a `force` attribute like this for pvcs (https://ant.apache.org/manual/Tasks/pvcstask.html) but I don't know if it's available as a command line option to you. – C.Evenhuis Mar 04 '19 at 07:17
  • 1
    Have you tried `myProcess.StandardInput.WriteLine("y");` before you call `myProcess.Start()`? (Just spit balling, I'm not really familiar with the API) – Benno Straub Mar 04 '19 at 07:21
  • @C.Evenhuis: I thought same but still couldn't figure it out. – Amir Mar 04 '19 at 07:22
  • Your best bet is probably to check for the file's existence and delete it before launching pcli. – John Wu Mar 04 '19 at 07:31
  • @BennoStraub: that is good trick and I think it is my answer, when I added your command I am getting this error :"StandardIn has not been redirected." I already enabled standardIn like this:=> startInfo.RedirectStandardInput = true; – Amir Mar 04 '19 at 07:37
  • @JohnWu: I want to keep the exsiting version label therefore I need to press N – Amir Mar 04 '19 at 07:38
  • @BennoStraub: your suggestion is correct, post your answer and I will approve it,tq. – Amir Mar 04 '19 at 07:44

1 Answers1

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

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

    ...
}

Note: This approach is not very reusable.

Using a command line option like /no-confirm (as John Wu suggested in the questions comments) is preferable, if it exists.

Amir
  • 1,919
  • 8
  • 53
  • 105
Benno Straub
  • 2,268
  • 3
  • 19
  • 21