Following is my code:
private static bool Register(string fullFileName, string username, SecureString password)
{
var isRegistered = false;
using (var process = new Process())
{
var startInfo = process.StartInfo;
startInfo.FileName = "regsvr32.exe";
startInfo.Arguments = string.Format("/s {0}", fullFileName);
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.ErrorDialog = true;
process.EnableRaisingEvents = true;
startInfo.Verb = "runas";
startInfo.UserName = username;
startInfo.Password = password;
try
{
isRegistered = process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message );
}
}
return isRegistered;
}
}
If I am commenting out the following:
startInfo.Verb = "runas";
startInfo.UserName = username;
startInfo.Password = password;
then it will not work since it need to be registered as admin. but the isRegistered will be set to true.
How can I know that it doesn't work?
Note: I am running it in silence mode so the user can't see the prompt. The error prompt is displayed when working in non-silent mode.
After the process.WaitForExit()
, you should check the process's exit code.
Process.ExitCode
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode%28v=vs.110%29.aspx
It is 0
if all succeeded. Non-zero if failed.