Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C# BAT to EXE  (Read 9263 times)

0 Members and 1 Guest are viewing this topic.

hibyy

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows 8
    C# BAT to EXE
    « on: July 23, 2009, 05:53:37 AM »
    Hello I figured out how to make a C# File that would rewrite a bat file in to a exe.
    I did this because
    1. I noticed a lot of pay for bat to exe programs.
    2. I was bored.

    this C# File uses the Text Props
    Make a Text called Props and put this in it if you want it to work.

    Code: [Select]
    E:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/csc.exe(The second first line is your csc path Auto makes the Props.txt now type your path into the exe now and it will be saved. change it if you need to)

    This file cannot compile files with a " "(Space) in the name!

    the CS file is
    (Edit it can use C# commands by putting ": " before the c# command in the bat file.)
    Code: [Select]
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    namespace Bat
    {
    public class EXE
    {
    [DllImport("msvcrt.dll")]
    public static extern int system(string cmd);
    public static int Exit()
    {
    try
    {
    return 1;
    }
    catch
    {
    return -1;
    }
    }
    public static void Main()
    {
    if (File.Exists("Props.txt")) {}else{Console.Write("CSC Path: "); string Pasd = Console.ReadLine(); StreamWriter GAS = new StreamWriter("Props.txt"); GAS.Write(Pasd); GAS.Close(); system("cls");}
    StreamReader NI = new StreamReader("Props.txt");
    string CSCP = NI.ReadLine();
    NI.Close();
    if (CSCP.Length <= 1) {Console.WriteLine("Enter your csc.exe path in Props.txt");Console.ReadKey(); Exit();}
    int Prop1 = 34;
    int Prop2 = 58;
    int Prop3 = 59;
    char QU = (char)(Prop1);
    char BS = (char)(Prop2);
    char SC = (char)(Prop3);
    system("Title Bat to Exe Compiler");
    Console.Write("Bat File: ");
    string BATt = Console.ReadLine();
    string BAT = BATt+".bat";
    StreamReader AddEnd = new StreamReader(@BAT);
    string ENDFILE = AddEnd.ReadToEnd();
    AddEnd.Close();
    StreamWriter Adding = new StreamWriter(@BAT);
    Adding.Write(ENDFILE);
    Adding.Write(Adding.NewLine);
    Adding.Write("THEEND");
    Adding.Close();
    StreamReader B = new StreamReader(@BAT);
    int BN = 0;
    int GO = 1;
    while (GO == 1)
    {
    system("CLS");
    Console.WriteLine("Compiling.");
    string BaT = B.ReadLine();
    string BPa = BAT+BN+".txt";
    StreamWriter BB = new StreamWriter(@BPa);
    BB.Write(BaT);
    BB.Close();
    BN++;
    system("CLS");
    Console.WriteLine("Compiling..");
    if (BaT == "THEEND")
    {
    GO++;
    }
    system("CLS");
    Console.WriteLine("Compiling...");
    }
    B.Close();
    StreamWriter BC = new StreamWriter("BAT.cs");
    int NB = 0;
    while (NB <= BN-2)
    {
    system("CLS");
    Console.WriteLine("Compiling.");
    string BPA = BAT+NB+".txt";
    StreamReader BR = new StreamReader(@BPA);
    string bat = BR.ReadLine();
    BR.Close();
    system("CLS");
    Console.WriteLine("Compiling..");
    if (NB == 0)
    {
    char bbB = bat[0];
    if (bbB == BS)
    {
    string bs = BS+" ";
    string Command = bat.Replace(bs, " ");
    BC.Write(Command);
    }
    BC.Write("using System.Runtime.InteropServices"+SC+" namespace BAT { public class Write { [DllImport("+QU+"msvcrt.dll"+QU+")] public static extern int system(string cmd)"+SC+" public static void Main() {");
    BC.Write(BC.NewLine);
    }
    else
    {
    char bbb = bat[0];
    if (bbb == BS)
    {
    string bs = BS+" ";
    string Command = bat.Replace(bs, " ");
    BC.Write(Command);
    }
    else
    {
    BC.Write("system("+QU+bat+QU+")"+SC);
    }
    }
    NB++;
    system("CLS");
    Console.WriteLine("Compiling...");
    }
    BC.Write("}}}");
    BC.Close();
    int NN = 0;
    while (NN <= BN-1)
    {
    string BPAA = BAT+NN+".txt";
    File.Delete(BPAA);
    NN++;
    }
    string CSCC = CSCP+" /Nologo /out:"+BATt+".exe BAT.cs";
    system("cls");
    system(CSCC);
    File.Delete("BAT.cs");
    Console.WriteLine(BAT+" has been made in to "+BATt+".exe.");
    system("pause");
    StreamWriter Ax = new StreamWriter(@BAT);
    Ax.Write(ENDFILE);
    Ax.Close();
    }
    }
    }

    if you want to test it you can that is why I posted it here. =P
    Oh and at the end of the bat file you have to have the word exit otherwise this won't work.
    The above statement is false I have changed it so it works without the exit command written in.
    and the goto or set commands will not work in this exe maker.

    Examples

    Code: [Select]
    @echo off
    echo Hello
    pause
    becomes
    Code: [Select]
    using System.Runtime.InteropServices; namespace BAT { public class Write { [DllImport("msvcrt.dll")] public static extern int system(string cmd); public static void Main() {
    system("@echo off");system("echo Hello");system("pause");
    }}}
    and
    Code: [Select]
    @echo off
    : int GO = 1;
    : while (GO == 1)
    : {
    echo Hello
    : }
    pause
    becomes
    Code: [Select]
    using System.Runtime.InteropServices; namespace BAT { public class Write { [DllImport("msvcrt.dll")] public static extern int system(string cmd); public static void Main() {
    system("@echo off"); int GO = 1; while (GO == 1) {system("echo Hello"); }system("pause");
    }}}
    and one last example
    Code: [Select]
    : using System; using System.IO;
    @echo off
    title Make File
    : int B = 1;
    : if (B == 1) {
    : Console.Write("File - ");
    : string F = Console.ReadLine();
    : File.Create(F); }
    : else {
    set /p F=File -
    set /a L=%F%
    : }
    becomes
    Code: [Select]
    using System; using System.IO; using System.Runtime.InteropServices; namespace BAT { public class Write { [DllImport("msvcrt.dll")] public static extern int system(string cmd); public static void Main() {
    system("@echo off"); system("title Make File"); int B = 1; if (B == 1) { Console.Write("File - "); string F = Console.ReadLine(); File.Create(F); } else {  system("set /p F=File - "); system(""set /a L=%F%); }
    }}}
    « Last Edit: October 10, 2009, 03:48:30 PM by hibyy »

    clleady



      Rookie

      Thanked: 1
      Re: C# BAT to EXE
      « Reply #1 on: July 25, 2009, 07:31:09 PM »
      How complicated it is.I admire you very much.