Uninstalling a software with C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AkbarAzizi
    New Member
    • Oct 2009
    • 6

    Uninstalling a software with C#

    Hi,

    I am working on a program which must be used to install/uninstall some software. I am using ManagementObjec t together with a ManagementOpera tionObserver in order to update a progressbar. Everything works unless updating the progressbar.

    Is there anybody who has experience with this feauture?

    -Akbar Azizi
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    So not making an .MSI installer?

    Comment

    • AkbarAzizi
      New Member
      • Oct 2009
      • 6

      #3
      I am not making an .MSI installer.

      The software, my program must install/uninstall is Uniprint, Adobe Reader, and Citrix. My problem is that, when I get notification from my observer, and I try to update the progressbar, everything stucks. The progress event should - according to its specification - also tell me how many procent of the process is gone/left but context and Uppebound is always 0!

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        I think seeing your code for this part will help a lot

        Comment

        • AkbarAzizi
          New Member
          • Oct 2009
          • 6

          #5
          Here you go:

          Code:
          internal static void Progress(object Sender, System.Management.ProgressEventArgs Args)                    
          {
                // Tell the form to update its progressbar
                 _Owner.ProgressChangedDelegate(Sender, _Owner, Args);
          }
           
          internal static void Completed(object Sender, System.Management.CompletedEventArgs Args)
          {       
                 _Owner.ProgressCompletedDelegate(Sender, _Owner, Args);
          }
          
          ………..
          ………..
          
          
          private string Uninstall(string Path)
          {
             ManagementObject Product = new ManagementObject(Path);
             System.Management.ManagementOperationObserver WMIObserver;
          
             WMIObserver = new System.Management.ManagementOperationObserver();
           
             WMIObserver.Completed += new System.Management.CompletedEventHandler(Completed);
             WMIObserver.Progress += new System.Management.ProgressEventHandler(Progress);
                                                   
             Product.InvokeMethod(WMIObserver, "Uninstall", null);                        
          
             …
          }
          Last edited by tlhintoq; Oct 31 '09, 07:13 PM. Reason: [CODE] ...your code here...[/CODE] tags added

          Comment

          • AkbarAzizi
            New Member
            • Oct 2009
            • 6

            #6
            and if you need to see some other related functions:

            Code:
            private ManagementObject GetProduct(string Name)
            {
               SelectQuery allProductsQuery = new SelectQuery("Win32_Product");
               ManagementObjectSearcher allProducts =
                        new ManagementObjectSearcher(allProductsQuery);
             
               foreach (ManagementObject product in allProducts.Get())
               {
                  foreach (PropertyData property in product.Properties)
                  {
                     if (property.Name == "Name" &&
                         property.Value != null &&                   
                         property.Value.ToString().Contains(Name))
                     {
                         return product;
                      }
                   }
                }
             
                return null;
            }
            
            private string MakeUninstallString(ManagementObject product)
            {
               StringBuilder UninstallString = new StringBuilder();
             
               UninstallString.Append("Win32_Product.");
             
               foreach (PropertyData property in product.Properties)
               {
                  if (property.Name == "IdentifyingNumber")
                  {
                     UninstallString.Append(@"IdentifyingNumber=" + "\"");
                     UninstallString.Append(property.Value.ToString());
                     UninstallString.Append("\"");
                  }
                  else if (property.Name == "Name")
                  {
                     UninstallString.Append(@",Name=" + "\"");
                     UninstallString.Append(property.Value.ToString());
                     UninstallString.Append("\"");
                  }
                  else if (property.Name == "Version")
                  {
                     UninstallString.Append(@",Version=" + "\"");
                     UninstallString.Append(property.Value.ToString());
                     UninstallString.Append("\"");
                  }
            }
            
            .......
            .......
            
            ManagementObject Product = GetProduct(ProductName);
            
            string UninstallString = MakeUninstallString(Product);
            
            Res = Uninstall(UninstallString);
            Last edited by tlhintoq; Oct 31 '09, 07:14 PM. Reason: [CODE] ...your code here...[/CODE] tags added

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

              Comment

              • AkbarAzizi
                New Member
                • Oct 2009
                • 6

                #8
                Aha... that is cool. Thank you!

                Comment

                Working...