简体   繁体   中英

How to execute batch file on the same executable as the “main” C# application

I have a given C# executable and I want to execute a batch file inside this executable. The batch file sets environment variables, and some of these operations are conditional to other environment variables (IF conditions).

I want these environment variables to be set inside the executable itself, so that the environment variables are set on the current executable.

Absolutely every method that I can find on our friend google when I write something like “C# execute batch same process” seem to be based on the method System.Diagnostics.Process method, which executes the batch file on a different process.

Thank you

I have a console application. I want this console application to interpret the commands inside that batch file so that the environment variables set within that batch file are taken into account on the console application.

But you definitely don't want to write a batch script interpreter in C#.

If you want your console app to use the environment variables assigned in the batch script, then start your executable at the end of that batch script.

Your application will then inherit that environment, and therefore those environment variables.

If you can't alter said batch script, then create a new batch script that:

  • Runs the original batch script, then
  • Runs your executable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM