cmd参数设置和获取变量
cmd paramter set and get variables
我正在使用我的 C# 代码启动命令 window。
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);
我如何声明一个变量,将其递增 1 然后 return 结果全部通过 cmd 参数?
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
)来获取同一行更改的变量的内容。
我正在使用我的 C# 代码启动命令 window。
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);
我如何声明一个变量,将其递增 1 然后 return 结果全部通过 cmd 参数?
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
)来获取同一行更改的变量的内容。