Show message Box in .net console application

We can show a message box in a console application. But first include this reference in your vb.net or c# console application

System.Windows.Forms;

Reference:

To add reference in vb.net program right click (in solution explorer) on your project name-> then add reference-> then .Net-> then select System.Windows.Forms.
To add reference in c# program right click in your project folders shown in solution explorer on add references-> .Net -> select System.Windows.Forms.

then you can do the below code for c# console application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {


            MessageBox.Show("Hello World");
        }
    }
}

For the vb.net application you can simply code after inclusion of above mentioned reference

Module Module1

    Sub Main()
        MsgBox("Hello")
        Console.ReadKey()


    End Sub

End Module

Adapted from this answer to a related question.


To have a simple message box inside your console application you can follow the below steps.

  1. Create a property with attribute of

    using System.Runtime.InteropServices;
    [DllImport("User32.dll", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr h, string m, string c, int type);
    
  2. User the property to call the message box.

    MessageBox((IntPtr)0, "asdasds", "My Message Box", 0);
    
    using System;
    using System.Runtime.InteropServices;
    namespace AllKeys
    {
        public class Program
        {
            [DllImport("User32.dll", CharSet = CharSet.Unicode)]
            public static extern int MessageBox(IntPtr h, string m, string c, int type);
    
            public static void Main(string[] args)
            {
                MessageBox((IntPtr)0, "Your Message", "My Message Box", 0);
            }
        }
    }