【C#】调用cmd.exe并获取结果

 

using (Process process = new Process())
{
string inputScriptPath = officeBits == "32bit" ? $@"cd c:\Program Files (x86)\Microsoft Office\{officeVer}\" : $@"cd c:\Program Files\Microsoft Office\{officeVer}\";
process.StartInfo.FileName = @"cmd.exe";
process.StartInfo.WorkingDirectory = @"C:\Windows\System32\";
process.StartInfo.Verb = "runas";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine(inputScriptPath);
process.StandardInput.WriteLine("cscript ospp.vbs /dstatus");
process.StandardInput.WriteLine("exit");
process.StandardInput.AutoFlush = true;
string output = process.StandardOutput.ReadToEnd();
process.Close();
return output;

}

posted @ 2022-05-27 20:14  yzhyingcool  阅读(613)  评论(0编辑  收藏  举报