0

Basically i want the user to press a button and then the console will write all the appropriate lines for the user.

Here's the code i've written:

    private void button6_Click(object sender, EventArgs e)
    {
        Process Cmd = new Process();
        Cmd.StartInfo.FileName = @"C:\windows\system32\cmd.exe";
        Cmd.Start();
        StreamWriter sw = new StreamWriter(@"C:\windows\system32\cmd.exe");
        {
            sw.WriteLine = ("hello");
        }
    }

I tried StreamWriter but doesn't seem to be co-operating.

2 Answers2

0

you can do something like this

   Process Cmd = new Process();
            Cmd.StartInfo.FileName = @"makecert.exe"; // enter the correct path
            cmd.StartInfo.Argument = "" // pass your aarguemnt
            Cmd.Start();
Viru
  • 2,228
  • 2
  • 17
  • 28
  • i'm getting a red line under "sw.WriteLine" – Callum Glenn Brankin Mar 02 '16 at 09:37
  • what does the red line say?? – Viru Mar 02 '16 at 09:37
  • The error is saying "Cannot Assign to 'WriteLine' because it is a 'mothod group'" – Callum Glenn Brankin Mar 02 '16 at 09:42
  • 1
    @CallumGlennBrankin Yes because your question had syntactical error....instead of calling writeline, you were asssing to it and I copied same code...I have corrected and it should work now.... – Viru Mar 02 '16 at 09:44
  • Viru I think you've got a typo in Cmd.StandarInput – Pikoh Mar 02 '16 at 09:45
  • 1
    @Pikoh Yes..Thank you...Corrected it – Viru Mar 02 '16 at 09:46
  • @Viru No Red lines but when i run it, it breaks at "StreamWriter sw = Cmd.StandardInput;" saying "An unhandled exception of type 'System.InvalidOperationExpection' Occured in system.dll" – Callum Glenn Brankin Mar 02 '16 at 09:53
  • @CallumGlennBrankin Thats because you have to set RedirectStandardInput to true and also set UseShellExecute to false. Check update – Viru Mar 02 '16 at 09:56
  • @Viru, Okay i've done that, now the command console opens then closes straight after. – Callum Glenn Brankin Mar 02 '16 at 10:01
  • I do not know what are you trying to achieve....you want to pass inout to CMD then you have to redirect input to CMD. and if you do that then you will not see the console...Probably if you tell me what you eventually want to do I can give you better answer – Viru Mar 02 '16 at 10:29
  • @Viru, i want the console to create certificates using OpenSSL. Instead of typing all the stuff in your self you hit a button and it does it for you. – Callum Glenn Brankin Mar 02 '16 at 10:32
  • @CallumGlennBrankin that you can do it using Arguments property of start info......I am assuming makecert.exe is waht you will beusing to craete certificate – Viru Mar 02 '16 at 10:34
0

What you want can't be done, because you need to redirect StandardInput to send commands to cmd and when you do this, the cmd window will close. You still don't explain what exactly you want to archieve, but i can only think of two options:

  • Create a batch file with all the commands you want to execute and pass it as an argument to Process.
  • Redirect StandardInput and StandardOutput. This way you should take care of showing all the cmd messages. This is a bit messy and could lead you to deadlocks.

Edit

So at last, what you want is just run a console application with parameters. This is a sample using makecert:

Process ppp = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = @"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\makecert.exe";
psi.Arguments = "-n 'CN=TempCA' -r -sv TempCA.pvk TempCA.cer";

ppp.StartInfo = psi;
ppp.Start();
Pikoh
  • 7,582
  • 28
  • 53
  • Basically i want to create a certificate using console so users who use my application can create their own certificates, basically i need the console to create the cert its self and to do that i need the command console to write stuff by its self. – Callum Glenn Brankin Mar 02 '16 at 10:19
  • I think that's different, then all you have to do is execute a .exe with parameters. That's not difficult, you just have to know the path to the exe file and pass the arguments you need in `Cmd.StartInfo.Arguments` – Pikoh Mar 02 '16 at 10:21
  • @CallumGlennBrankin see if updated answer fits your requirements – Pikoh Mar 02 '16 at 10:30