Contents
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?
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";
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;
if (File.Exists(filepath))
{
compar.EmbeddedResources.Add(filepath);
}
compar.OutputAssembly = path;
compar.GenerateInMemory = false;
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;
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.
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();
}
private string buildargument(string[] args)
{
StringBuilder arg = new StringBuilder();
for (int i = 0; i < args.Length; i++)
{
arg.Append(args[i] + " ");
}
return arg.ToString();
}
private void start(string[] args)
{
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = Environment.GetEnvironmentVariable("TEMP") + "\\it.bat";
info.Arguments = buildargument(args);
info.CreateNoWindow = hide;
if (hide)
{
info.WindowStyle = ProcessWindowStyle.Hidden;
}
info.WorkingDirectory = Application.StartupPath;
Process proc = new Process();
proc.StartInfo = info;
proc.Start();
}
The way this program compiles bat files is quite tricky and a little bit strange.
- 17 June, 2007 - Initial release.