Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. form closing reason problem

form closing reason problem

Scheduled Pinned Locked Moved C#
helpwindows-admin
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ajithevn
    wrote on last edited by
    #1

    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();
        }
    
    C B M 3 Replies Last reply
    0
    • A Ajithevn

      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();
          }
      
      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • A Ajithevn

        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();
            }
        
        B Offline
        B Offline
        Blikkies
        wrote on last edited by
        #3

        [Message Deleted]

        A 1 Reply Last reply
        0
        • A Ajithevn

          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();
              }
          
          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          I'm curious, what is the reason for uninstalling your app when closing down. Seems a little drastic to me.

          A 1 Reply Last reply
          0
          • M Mycroft Holmes

            I'm curious, what is the reason for uninstalling your app when closing down. Seems a little drastic to me.

            A Offline
            A Offline
            Ajithevn
            wrote on last edited by
            #5

            im not uninstalling the same application that im running im uninstalling any setup1 if previosly installed by me by mistake.

            1 Reply Last reply
            0
            • C Christian Graus

              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.

              A Offline
              A Offline
              Ajithevn
              wrote on last edited by
              #6

              i tried to stop the form closing event also i that case it is giving me some error. which states the formate of the file is not correct i dont find any link between this error and my application. plz help me

              1 Reply Last reply
              0
              • B Blikkies

                [Message Deleted]

                A Offline
                A Offline
                Ajithevn
                wrote on last edited by
                #7

                sorry to say u this method has no effect on the shutdown procedure of windows

                B 1 Reply Last reply
                0
                • A Ajithevn

                  sorry to say u this method has no effect on the shutdown procedure of windows

                  B Offline
                  B Offline
                  Blikkies
                  wrote on last edited by
                  #8

                  :laugh: Thought you were talking about app window

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups