简体   繁体   English

如何在与“主要” C#应用程序相同的可执行文件上执行批处理文件

[英]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. 我有给定的C#可执行文件,我想在此可执行文件中执行批处理文件。 The batch file sets environment variables, and some of these operations are conditional to other environment variables (IF conditions). 批处理文件设置环境变量,并且其中一些操作以其他环境变量(IF条件)为条件。

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. 当我编写类似“ C#执行批处理相同过程”之类的东西时,我在朋友谷歌上可以找到的每个方法似乎都基于System.Diagnostics.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#. 但是,您绝对不想用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. 运行可执行文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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