简体   繁体   English

如何在C#中运行下一个批处理文件命令

[英]How to run next batch file command in C#

I have two batch files, 我有两个批处理文件,

The first, contains multiple commands, each command with two parameters, and it calls batch2 eg 第一个包含多个命令,每个命令带有两个参数,它调用batch2,例如

C:\batch2 test1 "Hello this is a test"
C:\batch2 test2 "Hello this is a test as well"

The second, contains the .exe call, and takes in the parameters from batch1 第二个包含.exe调用,并从batch1接收参数

C:\test.exe %1 %2

The .exe does the stuff, and the end of it is the following: .exe可以完成这些工作,其结尾如下:

Console.WriteLine("complete", dir, output);
output.Close();
Console.Read();

The issue is, after the first line of batch1 is run, the second line is never run. 问题是,运行batch1的第一行后,再也不会运行第二行。 What can I add to the end of the .exe (the C# program) to keep batch1 running for each subsequent line? 我可以在.exe(C#程序)末尾添加什么,以使batch1在随后的每一行中运行?

Or, how can I keep batch1 running after the first line? 或者,如何使batch1在第一行之后运行?

You cannot add anything to the executable that would help you. 您无法任何可以帮助您的内容添加到可执行文件中。 It waits . 在等待 Best case is you can remove the line that waits. 最好的情况是您可以删除等待的行。

If you cannot, you can modify your batch file to just not wait for the executable to finish: 如果不能,则可以将批处理文件修改为不等待可执行文件完成:

start C:\test.exe %1 %2

That makes your two executables run in parallel. 这使您的两个可执行文件并行运行。

Or you could pipe a Return to your exe instead: 或者您可以通过管道将Return返回到您的exe:

echo.|C:\test.exe %1 %2

Hacky, but it should work. 哈克,但应该可以。

There seems to be a misunderstanding what a call is in batch vs C#. 与C#成批调用似乎是一种误解。

Actually from your code you don't call batch2 but simply run it, 实际上,从您的代码中您不会调用 batch2,而只是运行它,
what doesn't leave an option to return to batch1. 没有什么选择可以返回到batch1。 See call /? 看到call /? or visit http://ss64.com/nt/call.html 或访问http://ss64.com/nt/call.html

:: Batch1.cmd
@Echo off
Call "C:\batch2.cmd" test1 "Hello this is a test"
Call "C:\batch2.cmd" test2 "Hello this is a test as well"

:: Batch2.cmd
@Echo off
C:\test.exe %*

If you don't change any parameters in Batch2.cmd you can pass them all in one unchanged with %* 如果您不更改Batch2.cmd任何参数, Batch2.cmd可以使用%*将所有参数Batch2.cmd传递

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

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