cmd paramter设置并获取变量

本文关键字:获取 变量 设置 paramter cmd | 更新日期: 2023-09-27 18:19:55

我正在用C#代码启动一个cmd窗口。

string cmd = "/c echo test";
System.Diagnostics.Process process2 = new System.Diagnostics.Process();
process2.StartInfo.FileName = "cmd.exe";
process2.StartInfo.Arguments = cmd;
process2.StartInfo.CreateNoWindow = true;
process2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process2.StartInfo.UseShellExecute = false;
process2.StartInfo.RedirectStandardOutput = true;
process2.Start();
string output2 = process2.StandardOutput.ReadToEnd();
MessageBox.Show(output2);

如何声明一个变量,将其递增一,然后通过cmd参数返回结果?

cmd paramter设置并获取变量

cmd /c "set x=0 & set /a x+1"

您甚至不需要echo命令,因为在命令行上下文中,用于算术的set /a将输出结果。

如果你需要在结果后的行结束,那么

cmd /v /c "set x=0 & >nul set /a x+=1 & echo !x!"

这里需要延迟扩展(/v)来检索在同一行中更改的变量的内容。