0

Why compiling this create an exe which :

  • open a console
  • launch the form ?

What can be done for the runtime compiled form open alone, without console ?


//LIST OF USING
using System;
using System.Windows.Forms;
using System.CodeDom.Compiler;



 //CODE TO COMPILE
 string oSource = @"
  using System.Windows.Forms;
 using System;
 namespace fTest
  {
    public static class Program
    {
     public static void Main()
     {
     Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new MyForm());
     }
    }
  public  class MyForm:Form
    {
     public  MyForm()
     {
      this.Text=""Generated exe"";
     MessageBox.Show(""Generated exe says H3110 W0r1d"");
     }
    }    
  }";
  string compiledOutput="Generated.exe";

 //COMPILATION WORK
 String [] referenceAssemblies={"System.dll","System.Drawing.dll","System.Windows.Forms.dll"};

 CodeDomProvider _CodeCompiler = CodeDomProvider.CreateProvider("CSharp");
 System.CodeDom.Compiler.CompilerParameters _CompilerParameters =
  new System.CodeDom.Compiler.CompilerParameters(referenceAssemblies,"");

 _CompilerParameters.OutputAssembly = compiledOutput;
 _CompilerParameters.GenerateExecutable = true;
 _CompilerParameters.GenerateInMemory = false;
 _CompilerParameters.WarningLevel = 3;
 _CompilerParameters.TreatWarningsAsErrors = true;
 _CompilerParameters.CompilerOptions = "/optimize /target:winexe";//!! HERE IS THE SOLUTION !!

 string _Errors = null;
 try
 {
  // Invoke compilation
  CompilerResults _CompilerResults = null;
  _CompilerResults = _CodeCompiler.CompileAssemblyFromSource(_CompilerParameters, oSource);                                                

  if (_CompilerResults.Errors.Count > 0)
  {
   // Return compilation errors
   _Errors = "";
   foreach (System.CodeDom.Compiler.CompilerError CompErr in _CompilerResults.Errors)
   {
    _Errors += "Line number " + CompErr.Line +
    ", Error Number: " + CompErr.ErrorNumber +
    ", '" + CompErr.ErrorText + ";\r\n\r\n";
   }
  }
 }catch (Exception _Exception)
 {
  // Error occurred when trying to compile the code
  _Errors = _Exception.Message;
 } 



  //AFTER WORK
 if (_Errors==null)
 {
  // lets run the program
  MessageBox.Show(compiledOutput+" Compiled !");
  System.Diagnostics.Process.Start(compiledOutput); 
 }else
 {
  MessageBox.Show("Error occurred during compilation : \r\n" + _Errors);
 }
0

2 Answers 2

2

By default, csc compiles console applications. You need to add /target:winexe to compiler options.

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

Comments

1

have you tried adding "target:winexe" to the command line parameters?

Comments

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.