我如何使用C#运行命令提示符命令?可以说我想按顺序运行这些命令:cd F:/File/File2/...FileX/ipconfigping google.com或类似的东西…有人可以使用此方法:void runCommands(String[] commands) {//method guts.....
我如何使用C#运行命令提示符命令?
可以说我想按顺序运行这些命令:
cd F:/File/File2/...FileX/
ipconfig
ping google.com
或类似的东西…有人可以使用此方法:
void runCommands(String[] commands)
{
//method guts...
}
这样您的输入是一系列字符串命令(例如[“ ipconfig”,“ ping 192.168.192.168”,“ ping google.com”,“ nslookup facebook.com”),应在特定命令的单个命令提示符下执行将它们放入数组的顺序.谢谢.
解决方法:
that should be executed on a single command prompt in the specific
sequence in which they are put in the array
您可以在bat文件中编写命令序列,然后如下运行.
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "YOURBATCHFILE.bat";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Reference
本文标题为:如何使用C#轻松运行Shell命令?
- C#基于Twain协议调用扫描仪,设置多图像输出模式(Multi image output) 2023-03-29
- C# mysql 处理 事务 回滚 提交 2023-11-15
- .net core运行环境搭建 linux + windows 2023-09-28
- C#实现在listview中插入图片实例代码 2022-11-05
- C# Invoke,begininvoke的用法详解 2023-03-29
- UGUI轮播图组件实现方法详解 2023-01-16
- C# JavaScriptSerializer序列化时的时间处理详解 2022-11-10
- 基于WPF编写一个串口转UDP工具 2023-07-18
- C#使用Interlocked实现线程同步 2023-01-06
- c# – Windows窗体设计器 – 在类前自动添加命名空间 2023-09-19