69

I am very new to C#. I have just run C# 'Hello World' program using Visual Studio.

Can I run or compile a C# program without using Visual Studio?

If it is possible, then which compiler should I use?

Thanks

shim
  • 9,289
  • 12
  • 69
  • 108
A.s. Bhullar
  • 2,680
  • 2
  • 26
  • 32
  • 7
    [Command-line Building With csc.exe](http://msdn.microsoft.com/en-us/library/vstudio/78f4aasd.aspx) – MarcinJuraszek Aug 17 '13 at 08:46
  • You can, but Visual Studio is probably still the best option. – Surfbutler Aug 17 '13 at 08:57
  • 4
    You can write C# programs with Notepad. The point of using VS is to save yourself the time to figure out how to do that, avoid the large number of mistakes you'll make *and* lessen the considerable pain of finding the mistake. Particularly life without a good debugger is a wasted life. Spend your time wisely and use it to learn how to use the language and tools first. – Hans Passant Aug 17 '13 at 11:06
  • 2
    this question is usually asked because people can't afford the Licences fee for Visual studio, in which case you can now use studio for free with the community edition or you can use the an alternate IDE like sharp develop http://www.icsharpcode.net/opensource/sd/ though CE has stolen most of the impetus to keep 3rd party IDE's upto date – MikeT Sep 07 '16 at 11:46
  • 6
    I came to this question because I wanted a better understanding of how CSharp is run "under the hood". Visual studio might save you the time to figure out how to do that, but then you lose out on the knowledge of how it's done. – Josh Desmond May 29 '18 at 00:05
  • 1
    Not sure when this became available, but for 2019 and later, the official [`dotnet`](https://dotnet.microsoft.com/learn/dotnet/hello-world-tutorial#install) command line program should suit your needs. – wbadart Jan 03 '19 at 21:46

5 Answers5

97

If you have .NET v4 installed (so if you have a newer windows or if you apply the windows updates)

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe somefile.cs

or

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe nomefile.sln

or

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe nomefile.csproj

It's highly probable that if you have .NET installed, the %FrameworkDir% variable is set, so:

%FrameworkDir%\v4.0.30319\csc.exe ...

%FrameworkDir%\v4.0.30319\msbuild.exe ...
greg
  • 1,118
  • 1
  • 20
  • 40
xanatos
  • 109,618
  • 12
  • 197
  • 280
  • hello when i try to run program like this C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe hello.cs It gives an error can't create temporary file!! – A.s. Bhullar Aug 17 '13 at 12:55
  • Where did you created your .cs program? Did you create a folder like `C:\Foo`? Don't do it in the root. – xanatos Aug 17 '13 at 13:00
  • I have save the .cs program v4.0.30319 folder.. – A.s. Bhullar Aug 17 '13 at 13:12
  • 1
    @A.s.Bhullar Don't. Create a folder like C:\Something or D:\Something and copy your cs there. The subfolders of Windows are protected and normally you can't write there. – xanatos Aug 17 '13 at 13:19
  • I have a similar error, I believe it's because csc is trying to delete a temporary file in the v4.0.30319 folder and I don't have admin. my .cs file is on the desktop, I gave it the path inside double qoutes. What am I doing wrong? – Ryan Krage Dec 21 '15 at 10:29
  • @RyanKrage Have you put all your source files/csproj in a folder **outside** the `C:\Windows` and the `C:\Program Files` and the `C:\Program Files (x86)`, like for example `C:\MyProject`? – xanatos Dec 21 '15 at 10:37
  • @xanatos Yes, I have. The file ("hello world.cs") is on my desktop. I cd'd to "C:\Windows\Microsoft.NET\Framework\v4.0.30319" and then ran 'csc "C:\Users\ryankrage77\Desktop\hello world.cs" '. I got the following CSC410EBB9E235344628E8CC6F2373BD08E.TMP: error CS1567: Error generating Win32 resource: Access is denied. warning CS1610: Unable to delete temporary file 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\CSC410EBB9E235344628E8CC6F2373BD08E.TMP' used for default Win32 resource -- The system cannot find the file specified. – Ryan Krage Dec 21 '15 at 11:44
  • This answer only works for Windows – darw Oct 04 '20 at 18:00
42

I use a batch script to compile and run C#:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /out:%1 %2

@echo off

if errorlevel 1 (
    pause
    exit
)

start %1 %1

I call it like this:

C:\bin\csc.bat "C:\code\MyProgram.exe" "C:\code\MyProgram.cs"

I also have a shortcut in Notepad++, which you can define by going to Run > Run...:

C:\bin\csc.bat "$(CURRENT_DIRECTORY)\$(NAME_PART).exe" "$(FULL_CURRENT_PATH)"

I assigned this shortcut to my F5 key for maximum laziness.

Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108
13

There are different ways for this:

1.Building C# Applications Using csc.exe

While it is true that you might never decide to build a large-scale application using nothing but the C# command-line compiler, it is important to understand the basics of how to compile your code files by hand.

2.Building .NET Applications Using Notepad++

Another simple text editor I’d like to quickly point out is the freely downloadable Notepad++ application. This tool can be obtained from http://notepad-plus.sourceforge.net. Unlike the primitive Windows Notepad application, Notepad++ allows you to author code in a variety of languages and supports

3.Building .NET Applications Using SharpDevelop

As you might agree, authoring C# code with Notepad++ is a step in the right direction, compared to Notepad. However, these tools do not provide rich IntelliSense capabilities for C# code, designers for building graphical user interfaces, project templates, or database manipulation utilities. To address such needs, allow me to introduce the next .NET development option: SharpDevelop (also known as "#Develop").You can download it from http://www.sharpdevelop.com.

Elvin Mammadov
  • 25,329
  • 11
  • 40
  • 82
6

Another option is an interesting open source project called ScriptCS. It uses some crafty techniques to allow you a development experience outside of Visual Studio while still being able to leverage NuGet to manage your dependencies. It's free, very easy to install using Chocolatey. You can check it out here http://scriptcs.net.

Another cool feature it has is the REPL from the command line. Which allows you to do stuff like this:

C:\> scriptcs
scriptcs (ctrl-c or blank to exit)

> var message = "Hello, world!";
> Console.WriteLine(message);
Hello, world!
> 

C:\>

You can create C# utility "scripts" which can be anything from small system tasks, to unit tests, to full on Web APIs. In the latest release I believe they're also allowing for hosting the runtime in your own apps.

Check out it development on the GitHub page too https://github.com/scriptcs/scriptcs

cecilphillip
  • 11,446
  • 4
  • 36
  • 40
1

If you have a project ready and just want to change some code and then build. Check out MSBuild which is located in the Microsoft.Net under windows directory.

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "C:\Projects\MyProject.csproj" /p:Configuration=Debug;DeployOnBuild=True;PackageAsSingleFile=False;outdir=C:\Projects\MyProjects\Publish\

(Please do not edit, leave as a single line)

... The line above broken up for readability

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild "C:\Projects\MyProject.csproj"
/p:Configuration=Debug;DeployOnBuild=True;PackageAsSingleFile=False;
outdir=C:\Projects\MyProjects\Publish\
Willy satrio nugroho
  • 908
  • 1
  • 16
  • 27
Ramakant
  • 185
  • 1
  • 3
  • 10