Sign in to follow this  
warp X

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

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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();
}
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this