gpt4 book ai didi

c# - 如何使用 C# 轻松运行 shell 命令?

转载 作者:行者123 更新时间:2023-11-30 16:27:53 25 4
gpt4 key购买 nike

如何使用 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# - 如何使用 C# 轻松运行 shell 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com