Jump to content
  • Advertisement
Sign in to follow this  
warp X

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

This topic is 4890 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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?

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!