Click here to Skip to main content
Click here to Skip to main content

Bat file compiler

, 13 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An article deskribing how to convert a bat file to an executable.



This article shows how to compile a bat file into an executable file. The compiled executable can run without showing a window, and you can pass command line parameters to it as you would pass them to the bat file.

How the program works

The way this program compiles a bat file is quite tricky and weird, so I call this program a "Mock compiler". The bat file is not even parsed. This program creates another program, and adds the specified bat file as an embedded file in that program. When the generated program is executed, it extracts the embedded bat file to a temp folder, and runs it using the Process class. Simple, isn't it?

Using the code

In order to create another program from your application, you will need to create an instance of the CSharpCodeProvider class. The code snippet below shows how to do it:

using (CSharpCodeProvider code=new CSharpCodeProvider())
    CompilerParameters compar = new CompilerParameters();

    string option = "/target:winexe";

    // Set the icon for executable
    if (Properties.Settings.Default.Customicon && 
       option += " " + "/win32icon:" + "\"" + 
                 Properties.Settings.Default.iconpath + "\"";

    compar.CompilerOptions = option;
    compar.GenerateExecutable = true;
    compar.IncludeDebugInformation = false;

    //Add the bat file as an embedded resource
    if (File.Exists(filepath))

    compar.OutputAssembly = path;
    compar.GenerateInMemory = false; 

    //Add references

    compar.TreatWarningsAsErrors = false;

    //Compile it
    //The code is included in the executable as a resource
    CompilerResults res = 
              code.CompileAssemblyFromSource(compar, Properties.Resources.Program);

    if (res.Errors.Count > 0)
       result = false;
       result = true;

When you run the generated executable, it will extract the bat file, process the command line arguments if any, and run it. If specified, the bat file will run without creating any window. Here is the code that shows how this is accomplished.

//This code requires System.Reflection namespace
//Extracts the bat file
private void extract()
    string name = Assembly.GetExecutingAssembly().GetManifestResourceNames()[0];
    hide = name.EndsWith("hideit.bat");

    Stream theResource = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);

    BinaryReader br = new BinaryReader(theResource);
    FileStream fs = new FileStream(Environment.GetEnvironmentVariable("TEMP") + 
                    "\\it.bat", FileMode.Create);

    byte[] bt = new byte[theResource.Length];
    theResource.Read(bt, 0, bt.Length);
    fs.Write(bt, 0, bt.Length);


//Process command line arguments
private string buildargument(string[] args)
    StringBuilder arg = new StringBuilder();

    for (int i = 0; i < args.Length; i++)
       arg.Append(args[i] + " ");

    return arg.ToString();

//Start the process
private void start(string[] args)
    ProcessStartInfo info = new ProcessStartInfo();
    info.FileName = Environment.GetEnvironmentVariable("TEMP") + "\\it.bat";

    //Specify argument
    info.Arguments = buildargument(args);

    //Hide the window if specified
    info.CreateNoWindow = hide;
    if (hide)
        info.WindowStyle = ProcessWindowStyle.Hidden;

    //Set the working directory for the bat file. This is important as the
    //bat file might use relative path
    info.WorkingDirectory = Application.StartupPath;

    //At last start the process
    Process proc = new Process();
    proc.StartInfo = info;

Points of interest

The way this program compiles bat files is quite tricky and a little bit strange.


  • 17 June, 2007 - Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Giorgi Dalakishvili
Software Developer
Georgia Georgia
No Biography provided
Follow on   Google+

Comments and Discussions

Questioninteresting Pinmemberhdbbdh19-Mar-14 22:54 
GeneralMy vote of 5 Pinmemberhazekaizer5-Jan-12 4:27 
GeneralRe: My vote of 5 PinmemberMember 18269037-Feb-12 20:57 
QuestionAdd stuff... PinmemberBrandon T. H.5-Dec-11 3:30 
Generalwithout dos window PinmemberMember 219487819-Feb-11 9:45 
QuestionExe shown code Pinmembermohit kumar paliwal19-Feb-08 20:15 
GeneralUnusefull PinmemberIzzet Kerem Kusmezer13-Feb-08 22:09 
GeneralRe: Unusefull PinmemberBrandon T. H.5-Dec-11 3:34 
GeneralRe: G E N I A L, Sir !!!!!!!!!! PinmemberGiorgi Dalakishvili14-Nov-07 20:49 
GeneralTemp filename PinmemberGuido_d5-Nov-07 22:38 
GeneralRe: Temp filename PinmemberGiorgi Dalakishvili5-Nov-07 22:44 
AnswerRe: Temp filename Pinmemberlvorrtax14-Nov-07 14:09 
GeneralGood Idea. Pinmembernorm .net5-Nov-07 21:50 
GeneralRe: Good Idea. PinmemberGiorgi Dalakishvili5-Nov-07 22:44 
GeneralInteresting... PinmemberPIEBALDconsult22-Aug-07 5:54 
GeneralRe: Interesting... PinmemberGiorgi Dalakishvili22-Aug-07 5:58 
GeneralRe: Interesting... PinmemberMartyK200723-Sep-07 22:47 
GeneralRe: Interesting... PinmvpJ4amieC11-Oct-07 0:02 
GeneralRe: Interesting... PinmemberMartyK200711-Oct-07 0:06 
GeneralRe: Interesting... Pinmemberogrig11-Oct-07 15:05 
GeneralRe: Interesting... PinmemberMartyK200711-Oct-07 23:53 
GeneralRe: Interesting... PinmemberPCoffey13-Feb-08 10:23 
GeneralIt's pefect if encryption function added Pinmemberfrx23-Jul-07 22:26 
General[Message Deleted] PinmemberGiorgi Dalakishvili5-Aug-07 0:12 
GeneralRe: It's pefect if encryption function added Pinmvptoxcct6-Aug-07 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 13 Feb 2008
Article Copyright 2007 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid