3

I have designed my very own language that I, in the end, translate to C# code. Now I want to compile this C# code using the C# compiler through my Windows Forms Application.

Where is the actual compiler file located and how do I compile my code (currently placed in a string)??

9
  • 3
    See -> stackoverflow.com/questions/553143/… Commented Jul 16, 2012 at 9:40
  • 4
    @DarkXphenomenon: What is the point of that? You did not read the question correctly. Commented Jul 16, 2012 at 9:42
  • He wants to compile C#. CSC is a way to do that, from the command line, and hence can be invoked from within another C# program as well. Commented Jul 16, 2012 at 9:50
  • 2
    @MattDavey et al. Is the command prompt programmatic? No... Commented Jul 16, 2012 at 9:53
  • 2
    @leppie is right .. I need to compile the program from my C# Application not via the Command line manually..!! Commented Jul 16, 2012 at 9:55

6 Answers 6

5

It sounds like you are looking for the CSharpCodeProvider. http://support.microsoft.com/kb/304655, or MSDN have examples.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use C# CodeDom for this purpose. This link may help you

http://www.codeproject.com/Articles/3445/Runtime-Compilation-A-NET-eval-statement

Comments

0

You need to have a look at the following 2 sites:

1 - Command-line Building With csc.exe

http://msdn.microsoft.com/en-us/library/78f4aasd.aspx

and

2 - Determining Which Version of the .NET Framework Is Installed

http://msdn.microsoft.com/en-us/library/y549e41e.aspx

You will need to identify which version of the .NET Framework compiler you will use, from the second link you can see:

"You can install and run multiple versions of the .NET Framework on a computer. You can install the versions in any order. To see which versions are installed, view the %WINDIR%\Microsoft.NET\Framework directory. (You should also view the Framework64 directory on a 64-bit computer, which can have 32 or 64-bit versions installed.) Each version of the .NET Framework has a directory, and the first two digits of the directory name identify the .NET Framework version; for example: v1.1.4322 for the .NET Framework 1.1, v2.0.50727 for the .NET Framework 2.0, v3.5 for the .NET Framework 3.5, and so on."

1 Comment

I know that (CodeDom), but part of the question was "where the csc.exe is located"...
0
c:\windows\Microsoft.NET\Framework\v3.5\

contains the C# compiler csc.exe.

So, you could do something like the following:

const string outputfile = "abc.exe";
const string inputFile = "xyz.cs"

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "c:\\windows\\Microsoft.NET\\Framework\v3.5\\csc.exe";
startInfo.Arguments = "/out:" + outputFile + " " + inputFile;
Process.Start(startInfo);

1 Comment

(comments removed) I've removed some unhelpful comments; basically, some people have observed that it may not be necessary or appropriate to shell to csc to achieve this. It is a valid answer (assuming the paths are correct for the machine, etc), but it has been argued that there may be more direct ways to achieve this. </summary-of-comments>
0

just use nemerle compiler as service with csharp syntax.

Comments

-1

Have you considered using Emit instead?

The csc.exe compiler is part of the .Net:

The csc.exe executable is usually located in the Microsoft.NET\Framework\ folder under the system directory. Its location may vary depending on the exact configuration on any individual computer. Multiple versions of this executable will be present on the computer if more than one version of the .NET Framework is installed on the computer. For more information about such installations, see Determining Which Version of the .NET Framework Is Installed.

3 Comments

Why suggest Reflection.Emit?
-1 there is no reason to scell the csc.exe since codedom does exactly what asked.
While it is valid (I use Emit a lot), it is a bit like answering "how do I top up the oil in my engine?" with "first, you'll need a few blocks of engine-grade metal, a milling machine, a ..."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.