private async void btnInstallPackage_Click(object sender, EventArgs e)
{
string msg = await Task.Run(() => { return executeCmd(); });
UpdateMsg(msg);
}
public string executeCmd()
{
// 要执行的CMD命令 显示ip信息
string commandText = "ipconfig";
//KB5044619 安装windows补丁
//commandText = "dism /online /add-package /packagepath:E:\\Download\\windows10.0-kb5044619-x64_3ef2399e39c2d708f26d02aeb985c2c7f53f9a2c.cab";
// 启动进程
using (Process process = new Process())
{
process.StartInfo.FileName = "cmd.exe"; // 设置CMD为可执行文件
//process.StartInfo.Arguments = $"/c {commandText}";
process.StartInfo.RedirectStandardInput = true; // 允许写入输入流
process.StartInfo.RedirectStandardOutput = true; // 允许读取输出流
process.StartInfo.UseShellExecute = false; // 不使用系统外壳程序启动
process.StartInfo.CreateNoWindow = true; // 不创建新窗口
process.StartInfo.RedirectStandardError = true;
process.StartInfo.Verb = "runas"; // 设置以管理员身份运行
string strOutput = "start \r\n";
try
{
process.Start();
process.StandardInput.WriteLine(commandText);
process.StandardInput.WriteLine("exit");
strOutput += process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
catch (Exception ex)
{
strOutput += ex.Message;
}
UpdateMsg(strOutput);
return strOutput;
}
}