views:

146

answers:

3

i want to run a process programaticaly in c#. with process.start i can do it. but how can i prommt user when the process asks for some user input in between and continue again after providing the input.

+1  A: 

Just write to Process.StandardInput.

leppie
Note that you need to set ProcessStartInfo.UseShellExecute to false, and you must set ProcessStartInfo.RedirectStandardInput to true. Otherwise, writing to the StandardInput stream throws an exception.
John Buchanan
@John Buchanan: Thanks for repeating exactly (actually copying) what it says on the link I provided...
leppie
A: 

You can add an event handler to the OutputDataReceived event. This gets called whenever the process writes some data to its redirected output stream.

private StreamWriter m_Writer;

public void RunProcess(string filename, string arguments)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = filename;
    psi.Arguments = arguments;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    psi.UseShellExecute = false;

    Process process = Process.Start(psi);
    m_Writer = process.StandardInput;
    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new DataReceivedEventHandler(OnOutputDataReceived);
    process.BeginOutputReadLine();
}

protected void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
    // Data Received From Application Here
    // The data is in e.Data
    // You can prompt the user and write any response to m_Writer to send
    // The text back to the appication
}

In addition there is also a Process.Exited event to detect if your process Exits.

Rob Davey
+1  A: 

Here is a good article on executing a process synchronously and asynchronously from c#.

Binoj Antony