如何通过C#代码启动一个BAT文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1117792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start a BAT file through C# code
提问by
I want to execute a BAT file through the use of C# code.
我想通过使用 C# 代码执行一个 BAT 文件。
I attempted to use the following code,
我尝试使用以下代码,
Process aProcess = new Process();
aProcess = Process.Start(@"E:\IMP_DATA\PRC_Helper_uTest.bat");
aProcess.WaitForExit(24000);
aProcess.Close();
It starts the batch file but very next second stops.
它启动批处理文件,但下一秒停止。
I am not able to see any thing.
我什么都看不到。
Can anyone help me regarding this problem?
任何人都可以帮助我解决这个问题吗?
UPDATE
更新
Actually I want to start a new command prompt and run a batch file on that newly created command prompt.
实际上我想启动一个新的命令提示符并在新创建的命令提示符上运行一个批处理文件。
How could I achieve this?
我怎么能做到这一点?
回答by Dmitrii Lobanov
Add PAUSE in the end of .bat file?
在 .bat 文件的末尾添加 PAUSE?
回答by RC1140
Here is some code that might make a difference , set the process not to launch a shell using "aProcess.UseShellExecute = false;" and then redirect the output to a stream using
这是一些可能会有所不同的代码,使用“aProcess.UseShellExecute = false;”将进程设置为不启动shell 然后使用
aProcess.RedirectStandardError = true;
aProcess.RedirectStandardOutput = true;
string Results = aProcess.StandardOutput.ReadToEnd();
This should return the output that your batch file would show
这应该返回您的批处理文件将显示的输出
回答by Oliver Hanappi
Do you want the command prompt to stay open after the batch file has been executed? If so, then start "cmd.exe" with your batch file as argument, otherwise just put a pause command at the end of the batch file as it was said already before.
执行批处理文件后,您是否希望命令提示符保持打开状态?如果是这样,则以您的批处理文件作为参数启动“cmd.exe”,否则只需在批处理文件的末尾放置一个暂停命令,就像之前已经说过的那样。
Best Regards
此致
回答by Oliver
As you wrote in your update, you want to start a new command prompt and start a batch file on this prompt. But what you did, was starting a batch file.
正如您在更新中所写,您希望启动一个新的命令提示符并在此提示符下启动一个批处理文件。但是你所做的是启动一个批处理文件。
So to get this to work, start a command prompt, executing your batch file instead of execute your batch file (Sometimes it can be so simple ;-)).
所以为了让它工作,启动一个命令提示符,执行你的批处理文件而不是执行你的批处理文件(有时它可以很简单;-))。
Example:
例子:
private void StartCmdWithBatch(string nameOfBatchFile)
{
if (!File.Exists(nameOfBatchFile))
throw new FileNotFoundException(nameOfBatchFile);
string parameters = String.Format("/k \"{0}\"", nameOfBatchFile);
System.Diagnostics.Process.Start("cmd", parameters);
}
Further informations about command prompt options can be get by cmd /?
可以通过以下方式获取有关命令提示符选项的更多信息 cmd /?
回答by Artificial Intellegiance
I used this bat file with two C# codes here it is:
我在这里使用了带有两个 C# 代码的 bat 文件,它是:
@echo off
color 0a
mode 1000
:a
echo %random% %random% %random% %random% %random% %random% %random% %random%
goto a
Process aProcess = new Process();
aProcess = Process.Start(@"E:\IMP_DATA\PRC_Helper_uTest.bat");
aProcess.WaitForExit(24000);
aProcess.Close();
aProcess.RedirectStandardError = true;
aProcess.RedirectStandardOutput = true;
string Results = aProcess.StandardOutput.ReadToEnd();
And it worked perfectly fine.
它工作得非常好。