Click here to Skip to main content
11,716,249 members (87,545 online)
Click here to Skip to main content

Bat file compiler

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

Contents

Introduction

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 && 
        File.Exists(Properties.Settings.Default.iconpath))
    {
       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.EmbeddedResources.Add(filepath); 
    }

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

    //Add references
    compar.ReferencedAssemblies.Add("System.dll");
    compar.ReferencedAssemblies.Add("System.Data.dll");
    compar.ReferencedAssemblies.Add("System.Deployment.dll");
    compar.ReferencedAssemblies.Add("System.Drawing.dll");
    compar.ReferencedAssemblies.Add("System.Windows.Forms.dll");
    compar.ReferencedAssemblies.Add("System.Xml.dll");

    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;
    }
    else
       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);

    br.Close();
    fs.Close();
}

//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;
    proc.Start();
}

Points of interest

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

History

  • 17 June, 2007 - Initial release.

License

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

Share

About the Author

Giorgi Dalakishvili
Software Developer
Georgia Georgia
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questioninteresting Pin
hdbbdh19-Mar-14 21:54
memberhdbbdh19-Mar-14 21:54 
GeneralMy vote of 5 Pin
hazekaizer5-Jan-12 3:27
memberhazekaizer5-Jan-12 3:27 
GeneralRe: My vote of 5 Pin
Member 18269037-Feb-12 19:57
memberMember 18269037-Feb-12 19:57 
QuestionAdd stuff... Pin
Brandon T. H.5-Dec-11 2:30
memberBrandon T. H.5-Dec-11 2:30 
Generalwithout dos window Pin
Member 219487819-Feb-11 8:45
memberMember 219487819-Feb-11 8:45 
QuestionExe shown code Pin
mohit kumar paliwal19-Feb-08 19:15
membermohit kumar paliwal19-Feb-08 19:15 
GeneralUnusefull Pin
Izzet Kerem Kusmezer13-Feb-08 21:09
memberIzzet Kerem Kusmezer13-Feb-08 21:09 
GeneralRe: Unusefull Pin
Brandon T. H.5-Dec-11 2:34
memberBrandon T. H.5-Dec-11 2:34 
GeneralRe: G E N I A L, Sir !!!!!!!!!! Pin
Giorgi Dalakishvili14-Nov-07 19:49
memberGiorgi Dalakishvili14-Nov-07 19:49 
GeneralTemp filename Pin
Guido_d5-Nov-07 21:38
memberGuido_d5-Nov-07 21:38 
GeneralRe: Temp filename Pin
Giorgi Dalakishvili5-Nov-07 21:44
memberGiorgi Dalakishvili5-Nov-07 21:44 
AnswerRe: Temp filename Pin
lvorrtax14-Nov-07 13:09
memberlvorrtax14-Nov-07 13:09 
GeneralGood Idea. Pin
norm .net5-Nov-07 20:50
membernorm .net5-Nov-07 20:50 
GeneralRe: Good Idea. Pin
Giorgi Dalakishvili5-Nov-07 21:44
memberGiorgi Dalakishvili5-Nov-07 21:44 
GeneralInteresting... Pin
PIEBALDconsult22-Aug-07 4:54
memberPIEBALDconsult22-Aug-07 4:54 
GeneralRe: Interesting... Pin
Giorgi Dalakishvili22-Aug-07 4:58
memberGiorgi Dalakishvili22-Aug-07 4:58 
GeneralRe: Interesting... Pin
MartyK200723-Sep-07 21:47
memberMartyK200723-Sep-07 21:47 
GeneralRe: Interesting... Pin
J4amieC10-Oct-07 23:02
mvpJ4amieC10-Oct-07 23:02 
GeneralRe: Interesting... Pin
MartyK200710-Oct-07 23:06
memberMartyK200710-Oct-07 23:06 
GeneralRe: Interesting... Pin
ogrig11-Oct-07 14:05
memberogrig11-Oct-07 14:05 
GeneralRe: Interesting... Pin
MartyK200711-Oct-07 22:53
memberMartyK200711-Oct-07 22:53 
GeneralRe: Interesting... Pin
PCoffey13-Feb-08 9:23
memberPCoffey13-Feb-08 9:23 
GeneralIt's pefect if encryption function added Pin
frx23-Jul-07 21:26
memberfrx23-Jul-07 21:26 
General[Message Deleted] Pin
Giorgi Dalakishvili4-Aug-07 23:12
memberGiorgi Dalakishvili4-Aug-07 23:12 
GeneralRe: It's pefect if encryption function added Pin
toxcct6-Aug-07 2:24
mvptoxcct6-Aug-07 2:24 
GeneralRe: It's pefect if encryption function added Pin
Giorgi Dalakishvili6-Aug-07 2:44
memberGiorgi Dalakishvili6-Aug-07 2:44 
GeneralRe: It's pefect if encryption function added Pin
toxcct6-Aug-07 2:49
mvptoxcct6-Aug-07 2:49 
GeneralRe: It's pefect if encryption function added Pin
Member 834520528-Oct-11 11:29
memberMember 834520528-Oct-11 11:29 
GeneralMemoryStream Pin
windrago3-Jul-07 12:01
memberwindrago3-Jul-07 12:01 
GeneralRe: MemoryStream Pin
Giorgi Dalakishvili3-Jul-07 20:52
memberGiorgi Dalakishvili3-Jul-07 20:52 
AnswerRe: MemoryStream Pin
windrago6-Jul-07 9:38
memberwindrago6-Jul-07 9:38 
GeneralVery nice Pin
T1TAN27-Jun-07 22:13
memberT1TAN27-Jun-07 22:13 
GeneralRe: Very nice Pin
Giorgi Dalakishvili27-Jun-07 22:16
memberGiorgi Dalakishvili27-Jun-07 22:16 
GeneralJust What I Need Pin
/randz27-Jun-07 15:20
member/randz27-Jun-07 15:20 
GeneralRe: Just What I Need Pin
Giorgi Dalakishvili27-Jun-07 21:54
memberGiorgi Dalakishvili27-Jun-07 21:54 
Generaluseful Pin
MrGoodly17-Jun-07 11:52
memberMrGoodly17-Jun-07 11:52 
GeneralRe: useful Pin
mav.northwind17-Jun-07 19:12
membermav.northwind17-Jun-07 19:12 
GeneralRe: useful Pin
Giorgi Dalakishvili17-Jun-07 20:44
memberGiorgi Dalakishvili17-Jun-07 20:44 
GeneralRe: useful Pin
mav.northwind18-Jun-07 9:03
membermav.northwind18-Jun-07 9:03 
GeneralRe: useful Pin
Laubi23-Jun-07 6:06
memberLaubi23-Jun-07 6:06 
GeneralRe: useful Pin
Giorgi Dalakishvili25-Jun-07 20:29
memberGiorgi Dalakishvili25-Jun-07 20:29 
AnswerRe: useful Pin
Jim Koornneef19-Jun-07 3:27
memberJim Koornneef19-Jun-07 3:27 
GeneralRe: useful Pin
Giorgi Dalakishvili19-Jun-07 4:38
memberGiorgi Dalakishvili19-Jun-07 4:38 
GeneralRe: useful Pin
Stuart Dootson4-Jul-07 21:02
memberStuart Dootson4-Jul-07 21:02 
GeneralRe: useful Pin
Jeremy Falcon6-Aug-07 7:46
memberJeremy Falcon6-Aug-07 7:46 
Generalcool Pin
shota_gio17-Jun-07 8:47
membershota_gio17-Jun-07 8:47 
GeneralRe: cool Pin
Giorgi Dalakishvili17-Jun-07 9:10
memberGiorgi Dalakishvili17-Jun-07 9:10 

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
Web04 | 2.8.150901.1 | Last Updated 13 Feb 2008
Article Copyright 2007 by Giorgi Dalakishvili
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid