2

I am currently faced with a problem, I need to execute a batch script within a programs memory (so it does not have to extract the batch file to a temporary location).

I am open to solutions in C# and C++

Any help would be appreciated

4 Answers 4

5

cmd.exe won't run a script from the memory of your process. The options which seem most obvious to me are:

  1. Relax the constraint that stops you extracting the script to a temporary file.
  2. Compress your script into a single line and use cmd.exe /C to execute it. You'll need to use the command separator &&.
  3. Write your own batch command interpreter.
  4. Use a different scripting language.

Options 3 and 4 aren't really very attractive! Option 1 looks pretty good to me but I don't know what's leading to your constraint.

Sign up to request clarification or add additional context in comments.

1 Comment

Regarding option 2: the length of the command line is limited: stackoverflow.com/q/3205027/402322
3

Open a pipe to the command shell and write the program code into that pipe. Here is an example: http://support.microsoft.com/default.aspx?scid=kb;en-us;190351

3 Comments

Interesting idea. But is it really simpler than extracting the script into a temporary file?
The question was not what is simpler. The question was how to do it without writing the code into a file.
The link is dead.
3

In C# it's an easy way to use System.Diagnostics for the job. How!?

Basically, every batch command is an .exe file so you can start it in a separate process.

Some code:

        using System.Diagnostics;
        static void Main()
        {
           Process batch;

           batch = Process.Start("ping.exe", "localhost");
           batch.WaitForExit();
           batch.Close();

           batch = Process.Start("choice.exe", "");
           batch.WaitForExit();
           batch.Close();

           batch = Process.Start("ping.exe", "localhost -n 10");
           batch.WaitForExit();
           batch.Close();
      }

If you don't want to start every command in a separate process the solution is with a simple stream redirection.

      ProcessStartInfo startInfo = new ProcessStartInfo();
           startInfo.FileName = @"cmd.exe"; // Specify exe name.
           startInfo.UseShellExecute = false;
           startInfo.ErrorDialog = false;
           startInfo.RedirectStandardInput = true;

           //
           // Start the process.
           //
           Process process = Process.Start(startInfo);


           string[] batchFile = {"ping localhost",  "ping google.com -n 10", "exit"};

           int cmdIndex = 0;

           while (!process.HasExited) 
           {
               if (process.Threads.Count == 1 && cmdIndex < batchFile.Length)
               {
                   process.StandardInput.WriteLine(batchFile[cmdIndex++]);
               }
           }

Comments

1

What's a good way to write batch scripts in C#?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.