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.

50 lines
1.4 KiB
C#

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()
{
FileName = "cmd.exe",
Arguments = $"/u /k start cmd.exe /u /k {cmd}",
//RedirectStandardOutput = true, // 重定向标准输出
//StandardOutputEncoding = Encoding.UTF8,
UseShellExecute = true, // 不使用系统外壳程序启动
CreateNoWindow = false // 不创建新窗口
};
// 启动进程
using (Process? process = Process.Start(startInfo))
{
if (process == null)
{
return "执行命令出错";
}
}
return cmdResult;
}
}
}