You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
namespace HttpClientStudy.Core.Utilities
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// cmd命令行工具类
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class CmdUtility
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 执行cmd命令
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cmd"></param>
|
|
|
|
|
/// <returns>命令行输出结果</returns>
|
|
|
|
|
public static string RunCmd(string cmd)
|
|
|
|
|
{
|
|
|
|
|
string cmdResult = string.Empty;
|
|
|
|
|
|
|
|
|
|
// 创建一个新的ProcessStartInfo对象
|
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo("cmd", $"/c {cmd}")
|
|
|
|
|
{
|
|
|
|
|
RedirectStandardOutput = true, // 重定向标准输出
|
|
|
|
|
UseShellExecute = false, // 不使用系统外壳程序启动
|
|
|
|
|
CreateNoWindow = true // 不创建新窗口
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 启动进程
|
|
|
|
|
using (Process? process = Process.Start(startInfo))
|
|
|
|
|
{
|
|
|
|
|
if (process == null)
|
|
|
|
|
{
|
|
|
|
|
return "执行命令出错";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//process.WaitForExit();
|
|
|
|
|
|
|
|
|
|
// 读取cmd的输出
|
|
|
|
|
using (StreamReader reader = process!.StandardOutput)
|
|
|
|
|
{
|
|
|
|
|
cmdResult = reader.ReadToEnd();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmdResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|