How do I open a console from a Winform App?

F

Frank Rizzo

How do I open a console from a Winform App? I've tried
Console.WriteLine but that doesn't work.

Regards
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

You could start a cmd.exe process.

What is what you want to do exactly?

cheers,
 
F

Frank Rizzo

Ignacio said:
hi,

You could start a cmd.exe process.

What is what you want to do exactly?

cheers,

I want to print out some debug messages while the process is running
outside the IDE.
 
F

Frisky

Look up Log4Net. It can log debug messages to almost anything, you can
change the config file for the level of logging you want while your
application is running, and its free. (http://logging.apache.org/log4net/)

If you really want to open a Console, there are Win32 functions to open them
and what not. Look up AllocConsole, and FreeConsole in MSDN.

An even easier way is to use the www.sysinternals.com tool DebugView with
debugger messages. This also works as a source for Log4Net (see above). The
problem with this is that debug logging only occurs for debug builds. So no
logging in production environment.

Hope this helps...

Chuck
 
I

Ignacio Machin \( .NET/ C# MVP \)

hi,

cmd.exe is the MS-DOS command line, it's not the debug console you want.
You have a couple of options, IMO the easier/more convenient one is using
the event log.

cheers,
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You are right :)
More than once I have tried to run cmd.exe on a win9X :)

I don't know the difference though, nor why the two exists.

cheers,
 
W

Willy Denoyette [MVP]

Ignacio Machin ( .NET/ C# MVP ) said:
Hi,

You are right :)
More than once I have tried to run cmd.exe on a win9X :)

I don't know the difference though, nor why the two exists.

cmd starts the 32 bit NT OS command interpreter (sometimes mistakenly called
a DOS box), Command runs the 16 bit DOS command interpreter in a ntvdm.

Willy.
 
F

Frank Rizzo

So, bottom line. Is it possible to open the console from a WinForm app,
just like you would in a console app?

Thanks
 
W

Willy Denoyette [MVP]

Sure, you can start the 'cmd' interpreter using Process.Start and redirect
stdout, stdin and stderr to a TextBox control.

Willy.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I think that the OP want to do the opossite, write in the console window.
If that's so then you can't as the logs will be interpreted as commands.

You can use another form with a listbox for example, or a textbox ( in this
case you have to intercept the KeyPress event ).


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
 
F

Frank Rizzo

Ignacio said:
Hi,

I think that the OP want to do the opossite, write in the console window.
If that's so then you can't as the logs will be interpreted as commands.

You can use another form with a listbox for example, or a textbox ( in this
case you have to intercept the KeyPress event ).


cheers,

Thanks, that's exactly what I want to do. So it sounds like I can't do
this or not easily.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Of course you can do a similar thing, just that you cannot use the cmd.exe
program.
Do as I said, create a new form with a textbox, add a method like
WriteLog( ) that write in the textbox.
Intercept the KeyPress event of the control to allow only the cursor to be
used, all others will be "handled" by your method.


very easy indeed.


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top