[.net] Bringing up the console in a C# Windows.Forms app
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 suppose you could do a:
Interesting question. Is there another way that is better/cleaner/etc than this?
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();
}
}
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();
}
}
You could use the Trace.WriteLine stuff, and then you can attach to the process with anything that can debug processes.
Quote:Original post by paulecoyoteTrue, if the purpose is to debug/trace the .Net Framework provides much better ways than opening a console. For instance:
You could use the Trace.WriteLine stuff, and then you can attach to the process with anything that can debug processes.
* 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement