简体   繁体   English

在C#中执行批处理文件命令

[英]Executing batch file command within C#

I have the following code in my C# application which loaded a batch file silently using command prompt and executed and returned the result to a string: 我的C#应用​​程序中包含以下代码,该代码使用命令提示符以静默方式加载了批处理文件,并执行并将结果返回给字符串:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = @"C:\files\send.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);
proc.WaitForExit();

I am trying to avoid my application going out to a different file to execute the batch file, rather I wanted to embed it inside my application. 我试图避免我的应用程序访问另一个文件来执行批处理文件,而是想将其嵌入到我的应用程序中。 So I changed the above code to this: 所以我将上面的代码更改为:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "@ECHO ON java com.this.test567 send";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
proc.StartInfo = startInfo;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);

When the code executes, I can see the command prompt window for a brief moment before it closes and the execution is not working correctly. 代码执行时,我可以在关闭之前的短暂时间看到命令提示符窗口,并且执行无法正常工作。 How can I fix the issue? 我该如何解决该问题?

Instead of using cmd.exe just use java directly, you should also redirect standard error and check that after the process ends. 除了直接使用Java之外,还不应该使用cmd.exe,还应该重定向标准错误,并在过程结束后进行检查。

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = @"java.exe";
proc.StartInfo.Arguments = "com.this.test567";

proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
if(string.IsNullOrEmpty(strGetInfo))
    strGetInfo = proc.StandardError.ReadToEnd();
proc.WaitForExit();

Note that by calling cmd directly, you're effectively making a batch script with whatever you use for in the Arguments Property. 请注意,通过直接调用cmd,可以有效地使用Arguments属性中使用的内容制作批处理脚本。 Like a .bat file, the command window closes as soon as it's done. 就像.bat文件一样,命令窗口将在完成后立即关闭。 To fix this, add a pause command to the end. 要解决此问题,请在末尾添加暂停命令。

startInfo.Arguments = "@ECHO ON java com.this.test567 send\npause";
& seperates commands on a line.

&& executes this command only if previous command's errorlevel is 0.

|| (not used above) executes this command only if previous command's errorlevel is NOT 0

> output to a file

>> append output to a file

< input from a file

| output of one command into the input of another command

^ escapes any of the above, including itself, if needed to be passed to a program

so seperate commands with & 所以用&分开命令

"/k @ECHO ON&java com.this.test567&send"

/k keeps a window open. / k使窗口保持打开状态。

so you'll get in cmd 所以你会进入cmd

cmd /k @ECHO ON&java com.this.test567&send

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

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