form closing reason problem
-
Hi all on form closing event i check if it is windows shutdown and if yes i uninstall the program it is not working for me. i tried writting to a log file on form closing event if it is windows shutdown it worked. from another application on ckick of a button also the uninstall program worked. plz help me
private void ChatApplication_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{Uninstall(); } }
private static void Uninstall()
{
Process oProcess = new Process();
oProcess.StartInfo.FileName = "cmd.exe";
oProcess.StartInfo.CreateNoWindow = true;
oProcess.StartInfo.UseShellExecute = false;
RegistryKey oRegKey = null;
string sUninstallString = "";string\[\] asSubKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\").GetSubKeyNames(); foreach (string sSubKey in asSubKeys) { oRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + sSubKey); if (oRegKey.GetValue("Displayname") != null && oRegKey.GetValue("Displayname").ToString() == "Setup1") { sUninstallString = oRegKey.GetValue("Uninstallstring").ToString(); break; } } if (sUninstallString.LastIndexOf("exe") != (sUninstallString.Length - 3)) { sUninstallString = sUninstallString.Remove(0, 14); oProcess.StartInfo.Arguments = "/k msiexec.exe /x" + sUninstallString; } else { sUninstallString = sUninstallString.Replace("C:\\\\", ""); string sExe = sUninstallString.Substring(sUninstallString.LastIndexOf('\\\\') + 1); sUninstallString = sUninstallString.Remove(sUninstallString.LastIndexOf('\\\\')); oProcess.StartInfo.Arguments = ("/k cd\\\\ & cd " + sUninstallString + " & " + sExe); } oProcess.StartInfo.RedirectStandardError = true; oProcess.Start(); oProcess.WaitForExit(); oProcess.Close(); oProcess.Dispose(); }
-
Hi all on form closing event i check if it is windows shutdown and if yes i uninstall the program it is not working for me. i tried writting to a log file on form closing event if it is windows shutdown it worked. from another application on ckick of a button also the uninstall program worked. plz help me
private void ChatApplication_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{Uninstall(); } }
private static void Uninstall()
{
Process oProcess = new Process();
oProcess.StartInfo.FileName = "cmd.exe";
oProcess.StartInfo.CreateNoWindow = true;
oProcess.StartInfo.UseShellExecute = false;
RegistryKey oRegKey = null;
string sUninstallString = "";string\[\] asSubKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\").GetSubKeyNames(); foreach (string sSubKey in asSubKeys) { oRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + sSubKey); if (oRegKey.GetValue("Displayname") != null && oRegKey.GetValue("Displayname").ToString() == "Setup1") { sUninstallString = oRegKey.GetValue("Uninstallstring").ToString(); break; } } if (sUninstallString.LastIndexOf("exe") != (sUninstallString.Length - 3)) { sUninstallString = sUninstallString.Remove(0, 14); oProcess.StartInfo.Arguments = "/k msiexec.exe /x" + sUninstallString; } else { sUninstallString = sUninstallString.Replace("C:\\\\", ""); string sExe = sUninstallString.Substring(sUninstallString.LastIndexOf('\\\\') + 1); sUninstallString = sUninstallString.Remove(sUninstallString.LastIndexOf('\\\\')); oProcess.StartInfo.Arguments = ("/k cd\\\\ & cd " + sUninstallString + " & " + sExe); } oProcess.StartInfo.RedirectStandardError = true; oProcess.Start(); oProcess.WaitForExit(); oProcess.Close(); oProcess.Dispose(); }
You need to stop the form from closing ( by setting the event args ), then close it AFTER you have run your program. Even then, if windows is shutting down processes in general, I'm not sure it will work. You could perhaps put the uninstaller in the runonce registry key, so it uninstalls on startup.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
-
Hi all on form closing event i check if it is windows shutdown and if yes i uninstall the program it is not working for me. i tried writting to a log file on form closing event if it is windows shutdown it worked. from another application on ckick of a button also the uninstall program worked. plz help me
private void ChatApplication_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{Uninstall(); } }
private static void Uninstall()
{
Process oProcess = new Process();
oProcess.StartInfo.FileName = "cmd.exe";
oProcess.StartInfo.CreateNoWindow = true;
oProcess.StartInfo.UseShellExecute = false;
RegistryKey oRegKey = null;
string sUninstallString = "";string\[\] asSubKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\").GetSubKeyNames(); foreach (string sSubKey in asSubKeys) { oRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + sSubKey); if (oRegKey.GetValue("Displayname") != null && oRegKey.GetValue("Displayname").ToString() == "Setup1") { sUninstallString = oRegKey.GetValue("Uninstallstring").ToString(); break; } } if (sUninstallString.LastIndexOf("exe") != (sUninstallString.Length - 3)) { sUninstallString = sUninstallString.Remove(0, 14); oProcess.StartInfo.Arguments = "/k msiexec.exe /x" + sUninstallString; } else { sUninstallString = sUninstallString.Replace("C:\\\\", ""); string sExe = sUninstallString.Substring(sUninstallString.LastIndexOf('\\\\') + 1); sUninstallString = sUninstallString.Remove(sUninstallString.LastIndexOf('\\\\')); oProcess.StartInfo.Arguments = ("/k cd\\\\ & cd " + sUninstallString + " & " + sExe); } oProcess.StartInfo.RedirectStandardError = true; oProcess.Start(); oProcess.WaitForExit(); oProcess.Close(); oProcess.Dispose(); }
-
Hi all on form closing event i check if it is windows shutdown and if yes i uninstall the program it is not working for me. i tried writting to a log file on form closing event if it is windows shutdown it worked. from another application on ckick of a button also the uninstall program worked. plz help me
private void ChatApplication_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.WindowsShutDown)
{Uninstall(); } }
private static void Uninstall()
{
Process oProcess = new Process();
oProcess.StartInfo.FileName = "cmd.exe";
oProcess.StartInfo.CreateNoWindow = true;
oProcess.StartInfo.UseShellExecute = false;
RegistryKey oRegKey = null;
string sUninstallString = "";string\[\] asSubKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\").GetSubKeyNames(); foreach (string sSubKey in asSubKeys) { oRegKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + sSubKey); if (oRegKey.GetValue("Displayname") != null && oRegKey.GetValue("Displayname").ToString() == "Setup1") { sUninstallString = oRegKey.GetValue("Uninstallstring").ToString(); break; } } if (sUninstallString.LastIndexOf("exe") != (sUninstallString.Length - 3)) { sUninstallString = sUninstallString.Remove(0, 14); oProcess.StartInfo.Arguments = "/k msiexec.exe /x" + sUninstallString; } else { sUninstallString = sUninstallString.Replace("C:\\\\", ""); string sExe = sUninstallString.Substring(sUninstallString.LastIndexOf('\\\\') + 1); sUninstallString = sUninstallString.Remove(sUninstallString.LastIndexOf('\\\\')); oProcess.StartInfo.Arguments = ("/k cd\\\\ & cd " + sUninstallString + " & " + sExe); } oProcess.StartInfo.RedirectStandardError = true; oProcess.Start(); oProcess.WaitForExit(); oProcess.Close(); oProcess.Dispose(); }
I'm curious, what is the reason for uninstalling your app when closing down. Seems a little drastic to me.
-
I'm curious, what is the reason for uninstalling your app when closing down. Seems a little drastic to me.
-
You need to stop the form from closing ( by setting the event args ), then close it AFTER you have run your program. Even then, if windows is shutting down processes in general, I'm not sure it will work. You could perhaps put the uninstaller in the runonce registry key, so it uninstalls on startup.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.