Search code examples
c#batch-filebuildrelease

How do i include batch files inside of my .exe Instead of needing them in the folder the .exe is located in


So been searching or the web but can't seem to find an answer that has helped me. I have been looking for almost a week now.

I created a program in vs, alongside with some batch files. The Batch files run great by themselves and through the debug/release when including them in the folder with the .exe.

My problem is I want to be able to ONLY have the .exe file from my release and it still work.

Is there a way i can build these files inside the .exe? I have tried using c# to write my console commands instead of including seperate batch files. But im pretty new to c# and i get nothing but errors with the commands i want to run/if i run to many lines.

I would much rather have just c# instead of including the batch files but that I can't seem to figure out a solution to either.

Any help would be appreciated.

This is me currently calling batch files which works just fine. Again, if there is a way to just write this in c# instead of calling a batch file I would be happy to learn.

  Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo();
            psi.CreateNoWindow = false;
            psi.Verb = "runas";
            psi.FileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/" + "undo.bat";
            psi.UseShellExecute = true;
            process.StartInfo = psi;
            _ = process.Start();
            process.WaitForExit();

I'm starting CyberSecurity soon and am playing around with some Security stuff on my computer. Below is a sample code from my batch file to enable Security Protocols. If anything how would i write this in c#?

echo ...
echo Enabling Windows Firewall

netsh advfirewall set allprofiles state on
echo Enalbing HyperVisor

bcdedit /set hypervisorlaunchtype auto

echo Enabling UAC

%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 1 /f
echo.
echo.
echo Your Computer will now be restarting for changes to take effect! 

timeout 10
shutdown /r /t 001

Solution

  • What you can do is include the batchfiles as embedded resources in your project. Then read them and then execute them.

    to include them as embedded resources example...

    1. add them to your project.
    2. right click and go to properties
    3. select embedded resource

    then to extract...

    Write file from assembly resource stream to disk

    you can then write the file to disk and create process on it. or there is a way to execute cmd.exe without writing the file to disk but this is a little complicated so the best way is to just write to disk.

    Execute BATCH script in a programs memory