1

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
Robin Burk
  • 41
  • 3
  • You can try to feed them to a cmd process via StandardInput. – TaW Jan 20 '21 at 13:42
  • Example : `void runBatchFromText(string txt) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = @"cmd.exe"; // Specify exe name. startInfo.UseShellExecute = false; startInfo.ErrorDialog = false; startInfo.RedirectStandardInput = true; Process process = Process.Start(startInfo); string[] batchFile = txt.Replace("\r\\n", "\r").Split('\r'); int cmdIndex = 0; while (!process.HasExited) { if (process.Threads.Count == 1 && cmdIndex < batchFile.Length) { process.StandardInput.WriteLine(batchFile[cmdIndex++]);} }}` – TaW Jan 20 '21 at 13:42
  • I tried your given example, it gives an error saying .txt is not in the current context. Do i need to declare something else? is this just calling the existing batch file? – Robin Burk Jan 20 '21 at 13:57
  • What if i scratch the idea of the batch file and use write.line? Im just not to familiar with this yet – Robin Burk Jan 20 '21 at 14:02
  • https://learn.microsoft.com/en-us/windows/win32/menurc/finding-and-loading-resources. You compile the text in the app's resource. Or easier get you program to write to disk from constants. – user14797724 Jan 20 '21 at 19:03

2 Answers2

1

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

JackSojourn
  • 324
  • 2
  • 15
  • So this is helping for sure but im not sure how to implement this since im still new to all this. So i added them as embedded files. Now to ride them to a space on my disk i would use something like this? File.WriteAllBytes(@"C:\ResourceName", (byte[])Resources.resourcename); This isnt working though. Once its written i can then call it as before. But once thats done how would i terminate the file after completion of use? – Robin Burk Jan 20 '21 at 15:28
0

I followed the guide given above and a few others to get my solution to work. Embed the resource that's in your solution, then I used the following code to pretty much create the functions of being able to write it.

private static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName) 
        {

            Assembly assembly = Assembly.GetCallingAssembly();

            using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName)) 
                using (BinaryReader r = new BinaryReader(s))
                    using (FileStream fs = new FileStream(outDirectory + "//" + resourceName, FileMode.OpenOrCreate))
                        using (BinaryWriter w = new BinaryWriter(fs))
                            w.Write(r.ReadBytes((int)s.Length));

        }

Here is what I used to save, execute then delete the file.

    Extract("nameSpace", "outDirectory", "internalFilePath", "resourceName");
    Process process = new Process();
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.CreateNoWindow = false;
    psi.Verb = "runas";
    psi.FileName = @"C:/" + "resourceName";
    psi.UseShellExecute = true;
    process.StartInfo = psi;
    _ = process.Start();
    process.WaitForExit();
    System.Threading.Thread.Sleep(10);

    if ((System.IO.File.Exists(psi.FileName)))
    {
        System.IO.File.Delete(psi.FileName);
    }

Keep in mind im new when it comes to this so im sure there is a better way of writing it, but this worked for me!

Robin Burk
  • 41
  • 3