Building C# console project without Visual Studio
I need to creat开发者_运维问答e a script that produces an executable from a C# console project.
The client system where the script will be run doesn't have Visual Studio, but it has the .NET framework installed. How can it be done using a minimum or no software installation at the client place?
Does the C# compiler (csc.exe) come with .NET framework? If so, is there an environment variable for the location?
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
folder under the 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\
How to determine which versions and service pack levels of the Microsoft .NET Framework are installed
If you're a Grandma, this is easily:
1
-- Create a .bat
file with this:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe csharp.cs
csharp.exe
2
-- Create a .cs
file with this:
class Program
{
public static void Main()
{
System.Console.Clear();
//code...
System.Console.WriteLine("Congratulation! =D");
//...code
System.Console.ReadKey();
}
}
and run .bat file =)
I would HIGHLY recommend CS-Script. It's a simple installation that allows you to run CS-Files just like batch files (.bat).
Also, it allows you to compile the CS to an EXE file (console or window) from the context menu with a simple click and if you want Visual Studio as an editor, just one click, and it works.
It's really cool, I use it quite often, and I replaced all my "complex" batch scripts with CS-Scripts.
See How to programmatically compile code using the C# compiler.
Write a C# console application which takes two command line parameters: C# source file name and output .exe file name. In this program, open the C# source file, read it to a string and compile it by the way described in the article.
This console program can be used in the script that you need. A stand-alone C# compiler is not required, only the .NET Framework.
An alternative way is to install MonoDevelop or SharpDevelop. You get to use the .NET compilers within a free IDE.
Other than the above it is simply a matter of installing the compilers and using Notepad.
I think you can just install .NET Framework (CLR).
Taken from this post:
The C# compiler (csc.exe) is not part of Visual Studio but comes with the Microsoft.NET framework (on my system it is available at %systemroot%\Microsoft.NET\Framework\v2.0.50727). Once I discovered the whereabouts of the compiler, compiling the code was a straightforward operation.
Please refer to this guide in order to see how to work with csc: Command-line Building With csc.exe
精彩评论