简体   繁体   中英

rebuild wmi classes with c#

i am writing c# code to rebuild the wmi classes. i found that it can be done with the help of these command as discussed here :

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

i have tried passing these command in cmd and reading the output as :

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();
}

Can one share idea how to achieve this by code not manually.

I assume you have problems executing ms-dos commands, not reading the output.

Have you tried creating a batch file with those commands and executing it with system process class?

First of all, create the batch file with those commands and execute it manually. If it's works fine then just execute it as you done before.

System.Diagnostics.Process.Start("wmiBuilder.bat");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM