如何使用C#轻松运行Shell命令?
如何使用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”)应在将其放入数组中的特定顺序中以单个命令提示进行执行。谢谢。
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...
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在 bat 文件中编写命令序列并按如下方式运行。
参考
You could write command sequence in a bat file and run as below.
Reference
为此我编写了一个动态类Shell。您可以像这样使用它:
这是该类(它使用 System.Automation nuget 包来实现 powershell 功能):
I wrote a dynamic class Shell for this purpose. You can use it like so:
Here's the class (It uses the System.Automation nuget package for powershell features):
我不会为你填补空白(鱼),而是给你鱼竿:
http://msdn.microsoft.com/en-us/library /system.diagnostics.process.aspx
查看 Process 类。
I won't fill the blank (fish) for you but instead give you the rod:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
Check out the Process class.
您应该使用 .Net 等效项,可以在
System.IO
和System.Net
中找到。You should use the .Net equivalents, which can be found in
System.IO
andSystem.Net
.你想做什么?如果您正在考虑进行脚本并使用.NET框架,请查看
您可以在Powerhsell脚本中使用所有提到的命令 - CD,IPCONFIG,NSLOEGUP,PING等。
What are you trying to do? If you are looking at doing scripting and also use the .Net framework, have a look at Powershell
You can use all the commands that you mention as such in Powerhsell scripts - cd, ipconfig, nslookup, ping etc.
在这里您可以找到运行 shell 命令的解决方案源代码...它甚至考虑了 stderr。
但正如 @SLaks 指出的那样:是使用 .NET Framework 执行您所描述的操作的更好方法(即
System.IO
和System.Net
)!其他有趣的资源:
Here you can find a solution for running shell commands complete with source code... it even takes stderr into account.
BUT as @SLaks pointed out: there are better ways to do what you describe by using the .NET Framework (i.e.
System.IO
andSystem.Net
)!Other interesting resources: