Create Windows Application in C# from Command Line


Introduction

Here is trick to build and compile a C# Windows Forms application without using Visual Studio IDE.

This approach is not recommended but this is something you should know when you have only command line interface and want to create some handy utilities. If you do not have a Visual Studio IDE, you may download Express versions for free from MSDN website.

In this article, we are going to create a sample Windows Forms application that will create a Form with a Button control and click on the button will display a text using a MessageBox. 

Technologies:

.Net 2.0/3.5

Language:

C#

Prerequisite

1. .NET Framework 2.0/3.5
2. Visual Studio 2008/2005
3. Programatically Event Handling Knowledge   


Step 1.
Implementation


Fire up Notepad and write C# Program like below. As you can see from this code, we first import System and System.Windows.Forms namespaces.

//Declare Name Spaces
using System;
using System.Windows.Forms;

// Display Controls on Form Programatically

namespace TestGUI
{
class Test:Form
{
// Create Constuctor of Class

// Global Variables
public TextBox t1;
public Button b1;

Test()
{

//Suspend the layoutl logic
this.SuspendLayout();

//Set Properties of Button a
b1= new Button();
b1.Location =new System.Drawing.Point(30,40);
b1.Text = "Click Me";

//Add handler to Button
b1.Click += new System.EventHandler(b1_Click);


//Set Properties of Text Box
t1 = new TextBox();
t1.Location = new System.Drawing.Point(20,20);

//Add Conponents on form
this.Controls.Add(b1);
this.Controls.Add(t1);

//Give Form title
this.Text = "GUI Without VS";

//Resume the layout

this.ResumeLayout(false);

}

public void b1_Click(Object Sender,EventArgs e)
{
MessageBox.Show("Hello you Clicked Me");
}
//Now every thing is done Lets Build The Main method
public static void Main()
{
Application.Run(new Test());
}

}

}

Step 2. Compilation

Save the text file and name it GUITest.cs.

Now open Cmd and change directory to C:\Program Files\Microsoft Visual Studio 8\VC\

by

cd  C:\Program Files\Microsoft Visual Studio 8\VC\

command

then enter the command  to compile the exe here target is type of output we want and /out  is used to create our output file.

Command looks like this here you want to make sure the path of Exe and .cs files matches on your machine.

csc.exe /target:exe /out:c:\Users\Kirtan\Desktop\GUITest.exe C:\Users\Kirtan\Desktop\GUiTest.cs

3. Run

Now if you go to your desktop, you will see GUITest.exe is there. Just double click on that and run it.

4. Conclusion

The article explained how to create Windows applications using C# compiler from command line without using Visual Studio IDE.


Thank you


Similar Articles