none
Process class with complex batch RRS feed

  • Question

  • Hi,

    I want to wrap the openSSL cmd to a nice win Gui app.

    I tried to use "Process" class to invoke it.

    I built a batch which execute several commands in the OpenSSL prompt.

    I tried to execute it with Process class but I troubled  some difficulties with entering several commands one after another, when an <enter> is needed between each command.

    I.e I have a phase to enter a password and retype it, but how can I get the "Input Stream" of the keybord and control the input?

    I read at MSDN, and tried this:
                StreamWriter sw;
                StreamReader sr,se;
    
                ProcessStartInfo psi = new ProcessStartInfo("notepad");
    
                psi.UseShellExecute = false;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                            
    
                Process proc = new Process();
                proc.StartInfo = psi;
    
                proc.Start();
    
                sw = proc.StandardInput;
                sr = proc.StandardOutput;
                se = proc.StandardError;
    
                sw.AutoFlush = true;
    
                sw.WriteLine("6487");
    
                sw.Close();
    
                Console.WriteLine(sr.ReadToEnd())
    
    Nothing is written to the notepad :(

    Does anyone can guide me how to use this class?

    10x :)
    Sunday, October 25, 2009 11:59 PM

Answers

  • I did the below and worked OK for me under Windows 7 as a regular, non-admin user.  The setup is a simple WinForms application with a single label called label1 with AutoSize set to False and covering the surface of the form.  I ran the code from the Form_Load event.

                ProcessStartInfo psi = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe");
                psi.UseShellExecute = false;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                Process pr = Process.Start(psi);
                pr.StandardInput.WriteLine("dir");
                pr.StandardInput.WriteLine("exit");
                label1.Text = pr.StandardOutput.ReadToEnd();
                pr.Close();
    

    MCP
    • Marked as answer by eryang Monday, November 2, 2009 7:37 AM
    Tuesday, October 27, 2009 12:53 AM

All replies

  • As far as I know, output and input redirection is only good for console applications.  Notepad is a GUI application, and therefore cannot have its input or output redirected.
    MCP
    Monday, October 26, 2009 2:05 AM
  • I did the same thing with the openSSL cmd and I couldn't

    set any input for it.


    Maybe you have another idea?
    Monday, October 26, 2009 5:50 AM
  • I did the below and worked OK for me under Windows 7 as a regular, non-admin user.  The setup is a simple WinForms application with a single label called label1 with AutoSize set to False and covering the surface of the form.  I ran the code from the Form_Load event.

                ProcessStartInfo psi = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe");
                psi.UseShellExecute = false;
                psi.RedirectStandardInput = true;
                psi.RedirectStandardOutput = true;
                psi.RedirectStandardError = true;
                Process pr = Process.Start(psi);
                pr.StandardInput.WriteLine("dir");
                pr.StandardInput.WriteLine("exit");
                label1.Text = pr.StandardOutput.ReadToEnd();
                pr.Close();
    

    MCP
    • Marked as answer by eryang Monday, November 2, 2009 7:37 AM
    Tuesday, October 27, 2009 12:53 AM