2014年2月25日 星期二

C# - Console in Windows Form

Open Console in Windows Form

In Microsoft Wndows Form Application, console is closed, but you can open it by following snippet.

public partial class Form1 : Form
{

    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]

    static extern bool AllocConsole();

    public Form1(string[] args)
    {
        AllocConsole();

        InitializeComponent();
    }
}


Parameter usage
  In "main"
[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1(args)); // send args to Form1.cs
}
In Form1.cs
public partial class Form1 : Form
{
    public Form1(string[] args)
    {
        foreach (string parameter in args)
            System.Console.WriteLine(parameter);
    }
}

沒有留言:

張貼留言