Run interactive command line exe using c#
Asked Answered
M

3

14

I can run a command line process using process.start(). I can provide input using standard input. After that when the process demands user input again, how can my program know and pass the input to that exe?

Mellott answered 22/7, 2010 at 11:52 Comment(1)
You might be interested in this post covers many of the intricacies of working with .NET processes, particularly around working with input and outputGabler
C
15

There's an example here that sounds similar to what you need, using Process.StandardInput and a StreamWriter.

        Process sortProcess;
        sortProcess = new Process();
        sortProcess.StartInfo.FileName = "Sort.exe";

        // Set UseShellExecute to false for redirection.
        sortProcess.StartInfo.UseShellExecute = false;

        // Redirect the standard output of the sort command.  
        // This stream is read asynchronously using an event handler.
        sortProcess.StartInfo.RedirectStandardOutput = true;
        sortOutput = new StringBuilder("");

        // Set our event handler to asynchronously read the sort output.
        sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

        // Redirect standard input as well.  This stream
        // is used synchronously.
        sortProcess.StartInfo.RedirectStandardInput = true;
        sortProcess.Start();

        // Use a stream writer to synchronously write the sort input.
        StreamWriter sortStreamWriter = sortProcess.StandardInput;

        // Start the asynchronous read of the sort output stream.
        sortProcess.BeginOutputReadLine();
        Console.WriteLine("Ready to sort up to 50 lines of text");
        String inputText;
        int numInputLines = 0;
        do 
        {
            Console.WriteLine("Enter a text line (or press the Enter key to stop):");

            inputText = Console.ReadLine();
            if (!String.IsNullOrEmpty(inputText))
            {
                numInputLines ++;
                sortStreamWriter.WriteLine(inputText);
            }
        }

hope that helps

Crossways answered 22/7, 2010 at 12:16 Comment(5)
thanks again. but this is specific to sort.exe i m generating exe at run time for every user. i dont know which program he will type.Mellott
I think you can use "cmd.exe" instead of the executable they chose. It's a hack, but it might get the job doneCrossways
Dear robert can u plz explain how to use cmd.exe for my problemMellott
sortProcess.StartInfo.FileName = "cmd.exe"; might do the trickCrossways
I updated my stupid-simple .NET library project for running command line programs to include support for 'standard-input input' based on your answer – thanks!Declaratory
H
2

If I understand correctly I think you should be able to do this by redirecting the Standard Input/Output using RedirectStandardOutput and RedirectStandardInput.

The WinSCP project has a C# sample for how to communicate with it using this way. You can find it here: SFTP file transfers in .NET (though this sample only collects the output without using it at all, but the technique should be the same.)

Harrus answered 22/7, 2010 at 12:11 Comment(2)
thanks for ur reply. i tried it. but the process demanding user input is not predefined. the scenario is like 1. I want to design online compliler for c# 2. i can build the program using code dom n cretae exe 3. now i want to run that exe through c# 4. user can write any type of program which needs user input at diff stage. hope u understood whole scenarioMellott
@rajshades: I would have thought that would be similar though, just that instead of sending in all the commands in one go, you'd send in a command, read the output, send in the next command etc. Though I've not tested this. As a general note though, if you're creating the exe, can't you try to give it some better way of communicating with it (WCF, TCP/IP sockects, name pipes etc)?Harrus
B
1

You want to look up IPC. As robert showed above, the Process class in .NET will help you. But specifically for your problem (how to know when to write data): You can't. Not generically.

If you know the required input (e.g. "yyy"), you can supply it in the STDIN of the created process. You don't have to wait for the program to request this information: It will just read from STDIN when it wants data.

If you need to process the programs output to decide what to write to STDIN, try reading the processes STDOUT. You might run into problems with flushing, though...

Beka answered 22/7, 2010 at 12:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.