Search code examples
c#command-lineregsvr32

Retrieve results of regsvr32 execution from command line (without message boxes/popups)


C# WinForms application (.NET 4)

Directory path is selected from a combo(dropdown) menu and it is then included in the following command line statement.

for %f in ("< path >\*.ocx" "< path >\*.dll") do regsvr32 /s "%f"

where < path > is the directory path.

This executes fine. I would like to retrieve the registration successful messages (or errors) without the user having to click OK a thousand times to the popup / message box that displays. Obviously the silent (/s) switch gets rid of the popups.

What would be the best way to retrieve the results without the user seeing anything on their screen (besides the application itself)?

This is what I have right now,

public void reg_in_source_2()
{
    ProcessStartInfo cmdStartInfo = new ProcessStartInfo();
    cmdStartInfo.FileName = @"C:\Windows\System32\cmd.exe";
    cmdStartInfo.RedirectStandardOutput = true;
    cmdStartInfo.RedirectStandardError = true;
    cmdStartInfo.RedirectStandardInput = true;
    cmdStartInfo.UseShellExecute = false;
    cmdStartInfo.CreateNoWindow = true;

    Process cmdProcess = new Process();
    cmdProcess.StartInfo = cmdStartInfo;
    cmdProcess.ErrorDataReceived += cmd_Error;
    cmdProcess.OutputDataReceived += cmd_DataReceived;
    cmdProcess.EnableRaisingEvents = true;
    cmdProcess.Start();
    cmdProcess.BeginOutputReadLine();
    cmdProcess.BeginErrorReadLine();

    cmdProcess.StandardInput.WriteLine(@"for %%f in (""" + reference.source_folder + @"\*.ocx"" " + reference.source_folder + @"\*.dll"") do regsvr32 ""%%f""");
    cmdProcess.StandardInput.WriteLine("exit");

    cmdProcess.WaitForExit();
}

public void cmd_DataReceived(object sender, DataReceivedEventArgs e)
{
    reference.cmd_replies.Add(e.Data);
}

public void cmd_Error(object sender, DataReceivedEventArgs e)
{
    reference.cmd_replies_errors.Add(e.Data);
}

Solution

  • Instead of trying to write out a batch script to a cmd process, use Directory.GetFiles("c:\\somepath\\", "*.dll;*.ocx") to get the files you want to register - then use process.start to start regsvr32 processes (with the /silent argument) and check the return code to know if you were successful or not.

    If you try and do it in the script, you'll only get the return code of the cmd process, not of the regsvr32 processes which is what you're interested in.