0

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);
}
Ray Alex
  • 475
  • 2
  • 5
  • 17
  • did you try getting the exit code? typically if it's 0 that indicates success – Sten Petrov Mar 11 '13 at 15:52
  • http://csharptest.net/321/how-to-use-systemdiagnosticsprocess-correctly/ – Ilia G Mar 11 '13 at 15:56
  • I have not, do you have a link or example by chance? – Ray Alex Mar 11 '13 at 15:57
  • Looks like `reference.cmd_exit_code = cmdProcess.ExitCode;` after WaitForExit on process. How would this work though because I would need to know more than just if a single file did not register, I would need to know how many failed. I suppose I could start a registration process foreach file in directory... capture exit code and calculate results at end of total operation. – Ray Alex Mar 11 '13 at 16:04
  • 1
    Why are you writing out a batch script to the cmd process instead of just finding the files in code and then doing regsvr32 on those? – PhonicUK Mar 11 '13 at 16:06
  • Yeah that will surely be a better solution. Ill go that route PhonicUK – Ray Alex Mar 11 '13 at 16:09

2 Answers2

2

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.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
1

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.

Community
  • 1
  • 1
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39