ThexBasic ThexBasic - 6 months ago 29
C# Question

C# Code compiled .exe doesn't run

I am using the CSharpCodeProvider in order to compile an .exe myself with variable parameters. Compiling works fine (doesn't return errors) and succeeds but when running it starts and instantly quits w/o any error or output.
When changing the "Main" (e.g. to private or by renaming it) the compiler outputs that there is no valid Main Method, so the sample code shouldn't be the reason.

Does anyone have an answer/solution to this? I'm pretty lost on this one and would appreciate any helpful response.
Thanks in advance ~


compiled .exe output:


using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Resources;
using System.Security.Cryptography;
using System.Text;
using Microsoft.CSharp;
using Packer.Properties;

namespace Packer
internal class Program
private static void Main(string[] args)
Console.WriteLine("Sample Compiler");
Console.WriteLine(".ico-path: ");
var icon = "E:\\sample.ico"; //Console.ReadLine();
Console.WriteLine("> " + icon);
Console.WriteLine("Target-exe: ");
var target = "E:\\sample.exe"; //Console.ReadLine();
Console.WriteLine("> " + target);
var source = Resources.samplesource;
// Compile with all params
var success = CompileFromSource(source, target, icon);
// Determine result
Console.WriteLine(success ? "Successfully compiled." : "Compiling error.");
if (success) Process.Start(target);

private static bool CompileFromSource(string source, string output,
string icon = null, string[] resources = null)
var cParams = new CompilerParameters
GenerateInMemory = true,
WarningLevel = 0,
GenerateExecutable = true,
OutputAssembly = output
var options = "/optimize+ /platform:x86 /target:winexe /unsafe";
if (icon != null)
options += " /win32icon:\"" + icon + "\"";
// Set the options.
cParams.CompilerOptions = options;
cParams.TreatWarningsAsErrors = false;
// Check if the user specified any resource files. & Add them
if (resources != null && resources.Length > 0)
// Loop through all resource files specified in the Resources[] array.
foreach (var res in resources)
// Add each resource file to the compiled stub.
// Dictionary variable is used to tell the compiler what we want
var providerOptions = new Dictionary<string, string> {{"CompilerVersion", "v4.0"}};
var results = new CSharpCodeProvider(providerOptions).CompileAssemblyFromSource(cParams, source);
// Check if any errors occured while compiling.
if (results.Errors.Count <= 0) return true;
Console.WriteLine("The compiler has encountered {0} errors", results.Errors.Count);
foreach (CompilerError err in results.Errors)
Console.WriteLine("{0}\nLine: {1} - Column: {2}\nFile: {3}", err.ErrorText, err.Line, err.Column,
return false;

The code to compile:

using System;
using System.Text;

namespace CC2Runner
public static class Program
public static void Main(string[] args)
Debug.WriteLine("Sample Starting...");
Console.WriteLine("Sample Starting...");
catch (Exception e)


You've made a WinForms program.

This does not have a console, nor does it output anything to the console it was started from.

How do I know? This:

var options = "/optimize+ /platform:x86 /target:winexe /unsafe";

Which from csc:

 /target:exe                   Build a console executable (default) (Short
                               form: /t:exe)
 /target:winexe                Build a Windows executable (Short form:

Switch to /target:exe instead and it should work much better.