3

Title says it. I know I can use Process or ProcessStartInfo to run arguments, but I mean actually adding a command prompt control to my app (because I use it very often and it'd be convenient if it was already built-in.

Is there any way to do this other than coding a custom control? If not I can live with it, but it would definitely help.

david
  • 357
  • 2
  • 7
  • 18

3 Answers3

2

Something like this (not tested):

ProccessInfo pi = new ProccessInfo("cmd.exe");
pi.RedirectStandardError=true;
pi.RedirectStandardInput=true;
pi.RedirectStandardOutput=true;
Process cmd = Process.Start(pi);
cmd.StandardInput.WriteLine("Dir");
textBox1.Text = cmd.StandardOutput.ReadToEnd();

Watch out for deadlocks, those method can be blocking!

You can also use this solution from codeproject.com: http://www.codeproject.com/KB/miscctrl/commandprompt.aspx

Peter van Kekem
  • 1,387
  • 1
  • 12
  • 30
-1

See this question and related. Also this source code for example.

You can start a console near your win app's window which you will control and can output some data or get input from a user.

Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • I don't quit get it. Using AllocConsole() I can attach a console control to my application? But how does it work and how can I use it? – david Oct 21 '10 at 13:05
  • @david: `AllocConsole()` creates a console window near your main form and you can write their using standard `Console.WriteLine()` – abatishchev Oct 21 '10 at 13:38
  • I'm a bit busy right now but I will try the solution soon and accept your answer if it works :) – david Oct 21 '10 at 14:16
  • @david: Glad it helped! Vote up a bit please :) – abatishchev Oct 22 '10 at 13:18
-2

What is the problem with doing Console.ReadLine()/Console.WriteLine() in a loop? It is the most efficient way and you have fully control over what you are doing.

codymanix
  • 28,510
  • 21
  • 92
  • 151
  • 1
    Who voted my answer down? I consider this functionality trivial, why use other libraries which may cause problems and add dependencies (cmd.exe only works on windows, you can only execute other executables when having full trust). – codymanix Oct 21 '10 at 12:53
  • 1
    *rofl* I got an accepted -1 answer. Isn't there a special badge for this? – codymanix Oct 25 '10 at 13:56