Duch Czasu
Duch Czasu

Reputation: 3

C# - Use a console in the background without showing it

I've been thinking of giving the in Windows implemented cmd a fresh look and make a WinForm out of it(C# .net4.0 or later or latest mono# distribution). Now, what I plan on doing is:

I thought of some kind of "return" thing, like a dll would do, but I have not used consoles and forms together in a single project, so there's my question: How do I not show a console window but write commands into it's line and receive the output with a WinForms application?

Thanks in advance.

--EDIT I should maybe add that my main problem is typing catching the consoles output and also typing into it while it's not visible and thus not focusable.

Upvotes: 0

Views: 170

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70701

You may well run into difficulties other than simply suppressing the appearance of a console window. But as far as that particular requirement goes, it's not hard.

You'll use the System.Diagnostics.Process class to start the process. Before starting the process, you'll need to see ProcessStartInfo.CreateNoWindow property to true. Note that you also need to set ProcessStartInfo.UseShellExecute to false, otherwise the CreateNoWindow property is ignored.

As for the broader problem: it seems likely that you'll need to start the console process using "cmd.exe /k" to instantiate a new command-line interpreter process without it exiting before you're done with it. Then you'll also need to use the redirection features in the Process class to read from stdout and stderr, and to write to stdin.

Upvotes: 2

Related Questions