2

Is there a way to create and build an .exe from C#? (Editor's Note: Basically a .NET app that can create and compile other .NET apps - see accepted answer)

I am NOT talking about getting the .exe of my project (which is the only exe building I could find on google), I am talking about generating .exe files from within my program.

How would I create a runnable .exe file from C#? I have no idea how and on google I only find questions on where to find the .exe of the Project, while I want the project to build .exe's itself.

Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
John
  • 295
  • 1
  • 5
  • 16
  • That is how a .exe is made, by programming, the compiler is a VS build-in, so it created the file for you. – Max Dec 06 '13 at 15:41
  • compiling an exe is a very difficult process to do on your own. That's why we have Visual Studio to do it for us. – ddavison Dec 06 '13 at 15:41
  • The word you're looking for is "compiler". Search for "Roslyn", "Emit", "compiler as a service". – CodeCaster Dec 06 '13 at 15:41
  • 1
    You want to write a compiler ? – Francis Ducharme Dec 06 '13 at 15:41
  • Also, for more information see http://stackoverflow.com/questions/1495638/whats-in-a-exe-file – C.Evenhuis Dec 06 '13 at 15:42
  • Well your program could write code and compile it to a .exe using a command-line compiler – LittleSweetSeas Dec 06 '13 at 15:42
  • 2
    "Programmatically" is the word you want. You're asking how to generate an .exe programmatically. And poo on whoever downvoted you, it's a reasonable question. – 15ee8f99-57ff-4f92-890c-b56153 Dec 06 '13 at 15:43
  • Thanks for clearing it all up, I was just completely lost.. – John Dec 06 '13 at 15:44
  • @CodeCaster Ah that gives a lot of results, I guess I'm back on the right track again. – John Dec 06 '13 at 15:52
  • 1
    @CodeCaster It appears to me that he made a reasonable effort, but didn't know how to express what he was looking for. He just sounded like somebody who'd been wading through an awful lot of irrelevant google hits. You can ding people for not making an effort (and boy, do I ever), but not for making the *wrong* effort because they missed the obvious. We all miss the obvious sometimes. – 15ee8f99-57ff-4f92-890c-b56153 Dec 06 '13 at 16:00
  • @Ed you are totally right. What you described is exactly what I experienced. (I also had that a while ago, I wanted to read lines and react on that, and couldn't come up with the word 'compiler' ..) Though I understand it that people can't smell whether I didn't do my research or I'm really lost. – John Dec 06 '13 at 17:42
  • You should clarify from what you want to create the executable? From C# source code? From source code of another language? From compiled but unlinked files? – Phoenix Aug 19 '16 at 02:26

1 Answers1

7

The simplest way to do it without using external compilers is called CodeDOM, the relevant class you would use is called CSharpCodeProvider. There there are many tutorials on the internet (including one in the first link I gave) that will show you how to use it, the topic is too broad to explain in depth here.

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • OP says _"I am talking about generating .exe files within my program"_. Using CodeDOM is _a_ way to to that, not _the_ way. :) – CodeCaster Dec 06 '13 at 15:44
  • 1
    @CodeCaster changed the wording a bit. I agree with you there are other options, for example you could just call out to `csc.exe` and do it with that. – Scott Chamberlain Dec 06 '13 at 15:47