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

StackOverflow https://stackoverflow.com/questions/15342874

  •  23-03-2022
  •  | 
  •  

質問

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);
}
役に立ちましたか?

解決

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.

他のヒント

Please return the exit code from the console application by setting the Environment.Exit(code).

You can set the exit code as linked in this stackoverflow answer

The default value is 0 (zero), which indicates that the process completed successfully. Use a non-zero number to indicate an error. In your application, you can define your own error codes in an enumeration, and return the appropriate error code based on the scenario

All the error code can have status messages mapped to them, these messages can then be logged.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top