如何轻松地使用c#运行shell命令? [英] How to easily run shell commands using c#?

查看:859
本文介绍了如何轻松地使用c#运行shell命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用c#运行命令提示符命令?
我想说,我想按顺序运行这些命令:

how do i use c# to run command prompt commands? Lets say i want to run these commands in a sequence:

cd F:/File/File2/...FileX/
ipconfig
ping google.com

..可以有人做这个方法:

or something like that...Can someone make this method:

void runCommands(String[] commands) 
{
    //method guts...
}

,这样您的输入是一系列字符串命令(即ipconfig,ping 192.168.192.168,ping google.com,nslookup facebook.com),它们应该在单个命令提示符下以特定顺序在数组中被执行。感谢。

such that your input is a series of string commands (i.e ["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. Thanks.

推荐答案


应在单个命令提示符中执行
它们被放入数组中的顺序

that should be executed on a single command prompt in the specific sequence in which they are put in the array

您可以在bat文件中编写命令序列,并运行如下。

You could write command sequence in a bat file and run as below.

// 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();

参考

这篇关于如何轻松地使用c#运行shell命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆