我正在编写 c# 代码来重建 wmi 类。我发现可以借助此处讨论的这些命令来完成:
Regsvr32 %SystemRoot%\System32\wbem\wmidcprv.dll
cd /d %windir%\system32\wbem
for %i in (*.dll) do RegSvr32 -s %i
for %i in (*.exe) do %i /RegServer
我尝试在 cmd 中传递这些命令并将输出读取为:
Process objProcess = new Process();
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardOutput = true;
//objProcess.StartInfo.CreateNoWindow = true;
//objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
string cmd1=@"Regsvr32 %SystemRoot%\System32\wbem\wmidcprv.dll";
string cmd2=@"cd /d %windir%\system32\wbem";
string cmd3=@"for %i in (*.dll) do RegSvr32 -s %i";
string cmd4 = @"for %i in (*.exe) do %i /RegServer";
string strFinal = cmd1 + "\n" + cmd2 + "\n" + cmd3 + "\n" + cmd4;
//string cmd1=@"Regsvr32 %SystemRoot%\System32\wbem\wmidcprv.dll cd /d %windir%\system32\wbem for %i in (*.dll) do RegSvr32 -s %i for %i in (*.exe) do %i /RegServer";
objProcess.StartInfo.FileName = string.Format("cmd.exe");
objProcess.StartInfo.Arguments = string.Format(strFinal);
try
{
objProcess.Start();
}
catch
{
throw new Exception("Error");
}
StreamReader strmReader = objProcess.StandardOutput;
string strTempRow = string.Empty;
while ((strTempRow = strmReader.ReadLine()) != null)
{
Console.WriteLine(strTempRow);
}
if (!objProcess.HasExited)
{
objProcess.Kill();
}
可以分享一下如何通过代码而不是手动来实现这一点。