-1

I made a simple compiler to learn about CodeDom. But it does not work, when I try to open my compiled file it does nothing.

When I run the code and slect an dir to save the exe file the exe file is generated, but when I click the exe file it does nothing.

The builder:

using System;
using System.IO;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Windows.Forms;

namespace TestBuilder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void build(string output, string title, string msg)
        {
            CompilerParameters p = new CompilerParameters();
            p.GenerateExecutable = true;
            p.ReferencedAssemblies.AddRange(new String[] { "System.dll"});
            p.OutputAssembly = output;
            p.CompilerOptions = "/t:winexe";

            string source = File.ReadAllText(@"C:\Users\Gebruiker\Documents\visual studio 2015\Projects\TestCompiler\Test\Program.cs");
            string errors = String.Empty;
            source = source.Replace("[MESSAGE]", msg);
            CompilerResults results = new CSharpCodeProvider().CompileAssemblyFromSource(p, source);

            if (results.Errors.Count > 0)
            {
                foreach (CompilerError err in results.Errors)
                {
                    errors += "Error: " + err.ToString() + "\r\n\r\n";
                }
            }
            else errors = "Successfully built:\n" + output;
            MessageBox.Show(errors, "Build", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "EXE files (*.exe)|*.exe";
            DialogResult result = sfd.ShowDialog();
            if (result == DialogResult.OK)
            {
                build(sfd.FileName, textBox1.Text, textBox1.Text);
            }
        }
    }
}

The program.cs file:

using System;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("[MESSAGE]");
            Console.ReadLine();
        }
    }
}

How can I fix this problem, so when I compile a file and execute it, it shows the message I put in the textbox?

Gewoo
  • 437
  • 6
  • 19
  • Your code *does not* actually execute the compiler program. What are you asking for? Please be more specific than "does nothing". – Ondrej Tucny Feb 21 '16 at 18:41
  • "when I try to open my compiled file it does nothing." - ah, yeah. Please consider that we do not use telepathy here. What about you do not OPEN it, but EXECUTE it? What you mean with "compiled file"? – TomTom Feb 21 '16 at 18:44

1 Answers1

2

Change p.CompilerOptions = "/t:winexe"; to p.CompilerOptions = "/t:exe";.

After that the compiled program should output whatever you've put inside your TextBox when you run it.

Source

Use /target:exe to create a console application.

Szabolcs Dézsi
  • 8,743
  • 21
  • 29