[.net] Bringing up the console in a C# Windows.Forms app

Started by
4 comments, last by PDHB 18 years, 10 months ago
Is there a way to bring up the console in a Windows.Forms application? You can do it in C++ with the AllocConsole() function in a windowed app.
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
Advertisement
I suppose you could do a:
System.Diagnostics.Process.Start(@"c:\somepath\cmd.exe");


Interesting question. Is there another way that is better/cleaner/etc than this?
Ok, found it. Here's an example:

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices; // needed to call external
// application (winAPI dll)

namespace WindowsApplication1
{
partial class Form1: Form
{
public Form1()
{
InitializeComponent();
}

private void ViewConsole_CheckedChanged(object sender,
EventArgs e)
{
if (ViewConsole.Checked)
Win32.AllocConsole();
else
Win32.FreeConsole();
}
}

public class Win32
{
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
}
}
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
You could use the Trace.WriteLine stuff, and then you can attach to the process with anything that can debug processes.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
Quote:Original post by paulecoyote
You could use the Trace.WriteLine stuff, and then you can attach to the process with anything that can debug processes.
True, if the purpose is to debug/trace the .Net Framework provides much better ways than opening a console. For instance:

* Attach and use debugger
* Build with TRACE/DEBUG flags set, and use DebugView: http://www.sysinternals.com/ntw2k/freeware/debugview.shtml
* Build with TRACE/DEBUG flags set and add a TextWriterTraceListener: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdiagnosticstextwritertracelistenerclasstopic.asp
Make it a console application, and add the form?
Behold the nascent power of Semejant!

This topic is closed to new replies.

Advertisement