Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Compiling from the Command Prompt

This tutorial shows you how you can compile a simple piece of code from the C# command prompt compiler and how to add references.

On Friday, July 23rd 2004 at 04:52 AM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 26 votes)
Contextual Ads
More C# Resources
Advertisement
This tutorial will show you how to compile C# code from the DOS command prompt. It doesn't analyze all the parameters you can pass to the compiler and any other compiling methods, it only shows you how to compile a simple piece of code.



First we have to create a C# file that we shall compile:





namespace Form1

{

   public class Form1: System.Windows.Forms.Form

   {

      public Form1()

      {

         this.Text = "Handmade form";

      }

      public static void Main()

      {

         System.Windows.Forms.Application.Run(new Form1());

      }

   }

}



Paste this into Notepad and save it at C:\toCompile\Form1.cs.



Now from the Start Menu in the Microsoft Visual Studio .NET\Visual Studio .NET Tools program group, open Visual Studio .NET 2003 Command Prompt:





The command prompt compiler starts:





Setting environment for using Microsoft Visual Studio .NET 2003 tools.

(If you have another version of Visual Studio or Visual C++ installed and wish

to use its tools from the command line, run vcvars32.bat for that version.)



C:\Documents and Settings\Andrei>



We change to the toCompile directory using the command CD C:\toCompile at the command prompt:





C:\Documents and Settings\Andrei>CD C:\toCompile



C:\toCompile>



Now let's try to compile the program using the csc command. But first, we should not forget that we need to add some references like we usually do in Visual Studio.

Type csc /? in the command prompt to list the help associated with this command. You can see that using /reference we can add references for the compilation of the code:





/reference:<file list>  Reference metadata from the specified assembly files

                (Short from: /r)



Now that we know how to add a reference we can compile.





C:\toCompile>csc Form1.cs /reference:System.dll /reference:System.Windows.Forms.dll

Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4

for Microsoft (R) .NET Framework version 1.1.4322

Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.



That's it. You can now open the compiled application by typing Form1 because the file Form1.exe is in the toCompile folder.



When you run the application you'll see that a console window and an empty form will pop-up.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by sam on Sunday, May 8th 2005 at 11:32 AM

need betterhelp then this

by Amitesh on Tuesday, May 24th 2005 at 03:28 AM

There is no need to add the references!!!! Simply compiling with csc does it all

by kerry on Monday, August 1st 2005 at 09:46 AM

I couldnt figure out how to save the notepad document to( C:toCompileForm1.cs.) .I couldnt find the visual studio tools either in the start menu so this was totally useless for me..I really want to learn this stuff so I am really frustrated .

by Luke on Tuesday, September 20th 2005 at 07:14 AM

Please give me a detailed tutorial on how to compile programmes using c++ object oriented language.Thank you.

by serene0210 on Monday, September 26th 2005 at 04:43 AM

Since I am new to C#, this tutorial helped me a lot, thanks! But is there a way to compile the program using the DOS command prompt (not the Visual Studio .Net command prompt)? I tried using it, but the "csc" command is not recognized.

by Andrei Pociu on Monday, September 26th 2005 at 04:56 AM

Yes, you need to download the <a href="http://msdn.microsoft.com/netframework/downloads/framework1_1/" target="_blank">.NET Framework 1.1 SDK</a> first if you don't have it installed. Then you can compile from the command prompt just like you did with Visual Studio.

by Mark on Wednesday, February 8th 2006 at 06:27 AM

Hi, is there a way to see what "Compile command" visual studio generates when you launch the build command from the IDE?
Thanks

by Gopinath M on Friday, August 10th 2007 at 02:18 AM

When i refer an assembly using /r i got an error saying metadata can not be found. But the assembly which I referred is registered in GAC. But the problem is solved when i specify full path of the assembly in /r option. Can someone tell me why CSC is not able to locate the assembly available in GAC?

by karthick on Thursday, November 1st 2007 at 02:07 AM

Thanks!

But is there a way to compile the program using the DOS command prompt (not the Visual Studio .Net command prompt)? I tried using it, but the "csc" command is not recognized.

-Karthick

by on Monday, January 21st 2008 at 06:17 AM

d

by Srivani on Tuesday, April 22nd 2008 at 01:33 PM

Hi

Is there a way to create a reference to assembly from Dos Command prompt for C# project(.cproj) instead of .cs file using "csc" or any other command.

Thanks in Advance
-Srivani

by krishna kant on Wednesday, July 30th 2008 at 12:59 AM

thanks a lot...it helped a lot

by tom h on Sunday, August 17th 2008 at 10:05 AM

You don't need to use the visual studio command prompt, you can compile the program from any command prompt. You just need to include csc.exe in your path environmental variable and you can run it from any directory.

As for .csproj files, the C# compiler won't recognize them, they are a VS manifestation. If you want to compile a project that has multiple source files like class1.cs, class2.cs, and mainapp.cs (entry point), you would do:

csc.exe class1.cs class2.cs mainapp.cs

and it will output mainapp.exe

by cg on Thursday, September 25th 2008 at 08:33 PM

thanks.....

by Ram on Friday, September 26th 2008 at 01:32 PM

Good tutorial.

I have a class called MyClass.cs that has a statement to get data from web.config like below
var = System.Configuration.ConfigurationManager.AppSettings["KeyName"].ToString()

I compiled MyClass.cs successfully using C:\toCompile>csc /r:System.Configuration.dll MyClass.cs

I am getting runtime error "Object reference not set to an instance of an object".

Please help me how do you include the web.config and get data from it.

Thanks
Ram

by Andy on Friday, December 19th 2008 at 02:45 PM

I keep getting a message saying that my class "is not recognized as an internal or external command, operable program, or batch file."

My gut tells me there is a problem with my environment variables. Any ideas?

Thanks.

by suwarn on Thursday, March 5th 2009 at 12:39 AM

Hi,
I am not able to compile .cs file with visual studio 2008 command prompt. I am getting a error message that the system cannot find the path specified. I try to change the directry using CD command but it's not working.I am using windows vista. Can you please let me know why i am getting such error? Please help.

by S Sayen on Tuesday, March 17th 2009 at 06:15 AM

Thanks this helped me.

by menaka on Thursday, September 10th 2009 at 06:40 AM

how to include csc.exe in the path environment variable

by rebtut on Sunday, October 11th 2009 at 06:45 PM

If you want to compile from clipboard with csc see
http://reboltutorial.com/blog/redirect-shell-to-rebol-console/

by David on Wednesday, April 7th 2010 at 08:22 PM

Anybody have any idea how to convert a complex large VS.NET solution and/or project (VS.NET 2003, 2005, or 2008) into a command line build script such that you can build it from the command line, and ideally without need of having VS.NET installed, like just having the .NET framework is enough?

That would really be useful.

by satish on Friday, April 16th 2010 at 09:00 AM

please tel me how to compile the c# program using command prompt /step wise


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources