Downloading & Executing an Installer - User Cancels
-
I have an update process in my app that downloads an installer and runs it. If the user cancels the installer it throws a Win32Exception. The problem is that in the Catch all I get is the Exception with the message "The operation was cancelled by the user". Other than the text of the message, there's really no way to know for sure what exception occurred. I don't want to rely on the exception message. Any way to trap this specific issue? Here's my code:
private static void StartInstaller()
{
try
{
// Start the installler
_ = Process.Start(_localSetupFile);// Wait until it's started Process\[\] processes; do { processes = Process.GetProcessesByName(\_processName); Thread.Sleep(1000); } while (processes.Length == 0); } catch (Win32Exception e) { // Problem here } catch (Exception e) { // Handle the error throw; }
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I have an update process in my app that downloads an installer and runs it. If the user cancels the installer it throws a Win32Exception. The problem is that in the Catch all I get is the Exception with the message "The operation was cancelled by the user". Other than the text of the message, there's really no way to know for sure what exception occurred. I don't want to rely on the exception message. Any way to trap this specific issue? Here's my code:
private static void StartInstaller()
{
try
{
// Start the installler
_ = Process.Start(_localSetupFile);// Wait until it's started Process\[\] processes; do { processes = Process.GetProcessesByName(\_processName); Thread.Sleep(1000); } while (processes.Length == 0); } catch (Win32Exception e) { // Problem here } catch (Exception e) { // Handle the error throw; }
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Is the installer an .MSI?
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
I have an update process in my app that downloads an installer and runs it. If the user cancels the installer it throws a Win32Exception. The problem is that in the Catch all I get is the Exception with the message "The operation was cancelled by the user". Other than the text of the message, there's really no way to know for sure what exception occurred. I don't want to rely on the exception message. Any way to trap this specific issue? Here's my code:
private static void StartInstaller()
{
try
{
// Start the installler
_ = Process.Start(_localSetupFile);// Wait until it's started Process\[\] processes; do { processes = Process.GetProcessesByName(\_processName); Thread.Sleep(1000); } while (processes.Length == 0); } catch (Win32Exception e) { // Problem here } catch (Exception e) { // Handle the error throw; }
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Have you looked at ClickOnce? I have a Silent Updater for .Net Framework v3.5+ that takes out all of the hard work: Silent ClickOnce Installer for Winform & WPF in C# & VB[^]. If your app is Dot Net Core Framework, I have a new article that I will release very soon, just need to finish the article writeup.
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
-
I have an update process in my app that downloads an installer and runs it. If the user cancels the installer it throws a Win32Exception. The problem is that in the Catch all I get is the Exception with the message "The operation was cancelled by the user". Other than the text of the message, there's really no way to know for sure what exception occurred. I don't want to rely on the exception message. Any way to trap this specific issue? Here's my code:
private static void StartInstaller()
{
try
{
// Start the installler
_ = Process.Start(_localSetupFile);// Wait until it's started Process\[\] processes; do { processes = Process.GetProcessesByName(\_processName); Thread.Sleep(1000); } while (processes.Length == 0); } catch (Win32Exception e) { // Problem here } catch (Exception e) { // Handle the error throw; }
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Win32Exception has a NativeErrorCode property which should return 1223 (0x4C7) when the user stops a process starting at a UAC prompt. An example from my own code where I do something similar is probably clearer than more words!
private static void RestartWithRunAs(ProcessStartInfo psi) { psi.UseShellExecute = true; psi.ErrorDialog = true; psi.Verb = "runas"; try { Process.Start(psi); } catch (System.ComponentModel.Win32Exception exc) { // Accept cancelled by user as a valid action const Int32 CancelledByUser = 1223; if (exc.NativeErrorCode != CancelledByUser) { // MessageBox.Show(exc.ToString(), "Restart failure"); throw; } } }
See also System Error Codes (1000-1299) (WinError.h) - Win32 apps | Microsoft Learn[^] Alan.
-
Win32Exception has a NativeErrorCode property which should return 1223 (0x4C7) when the user stops a process starting at a UAC prompt. An example from my own code where I do something similar is probably clearer than more words!
private static void RestartWithRunAs(ProcessStartInfo psi) { psi.UseShellExecute = true; psi.ErrorDialog = true; psi.Verb = "runas"; try { Process.Start(psi); } catch (System.ComponentModel.Win32Exception exc) { // Accept cancelled by user as a valid action const Int32 CancelledByUser = 1223; if (exc.NativeErrorCode != CancelledByUser) { // MessageBox.Show(exc.ToString(), "Restart failure"); throw; } } }
See also System Error Codes (1000-1299) (WinError.h) - Win32 apps | Microsoft Learn[^] Alan.
Thanks. I'll give it a try
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I have an update process in my app that downloads an installer and runs it. If the user cancels the installer it throws a Win32Exception. The problem is that in the Catch all I get is the Exception with the message "The operation was cancelled by the user". Other than the text of the message, there's really no way to know for sure what exception occurred. I don't want to rely on the exception message. Any way to trap this specific issue? Here's my code:
private static void StartInstaller()
{
try
{
// Start the installler
_ = Process.Start(_localSetupFile);// Wait until it's started Process\[\] processes; do { processes = Process.GetProcessesByName(\_processName); Thread.Sleep(1000); } while (processes.Length == 0); } catch (Win32Exception e) { // Problem here } catch (Exception e) { // Handle the error throw; }
}
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.