Connecting Tech Pros Worldwide Help | Site Map

Uninstalling a software with C#

Newbie
 
Join Date: Oct 2009
Location: Denmark
Posts: 5
#1: 3 Weeks Ago
Hi,

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

Is there anybody who has experience with this feauture?

-Akbar Azizi
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#2: 3 Weeks Ago

re: Uninstalling a software with C#


So not making an .MSI installer?
Newbie
 
Join Date: Oct 2009
Location: Denmark
Posts: 5
#3: 3 Weeks Ago

re: Uninstalling a software with C#


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!
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#4: 3 Weeks Ago

re: Uninstalling a software with C#


I think seeing your code for this part will help a lot
Newbie
 
Join Date: Oct 2009
Location: Denmark
Posts: 5
#5: 3 Weeks Ago

re: Uninstalling a software with C#


Here you go:

Expand|Select|Wrap|Line Numbers
  1. internal static void Progress(object Sender, System.Management.ProgressEventArgs Args)                    
  2. {
  3.       // Tell the form to update its progressbar
  4.        _Owner.ProgressChangedDelegate(Sender, _Owner, Args);
  5. }
  6.  
  7. internal static void Completed(object Sender, System.Management.CompletedEventArgs Args)
  8. {       
  9.        _Owner.ProgressCompletedDelegate(Sender, _Owner, Args);
  10. }
  11.  
  12. ………..
  13. ………..
  14.  
  15.  
  16. private string Uninstall(string Path)
  17. {
  18.    ManagementObject Product = new ManagementObject(Path);
  19.    System.Management.ManagementOperationObserver WMIObserver;
  20.  
  21.    WMIObserver = new System.Management.ManagementOperationObserver();
  22.  
  23.    WMIObserver.Completed += new System.Management.CompletedEventHandler(Completed);
  24.    WMIObserver.Progress += new System.Management.ProgressEventHandler(Progress);
  25.  
  26.    Product.InvokeMethod(WMIObserver, "Uninstall", null);                        
  27.  
  28.    …
  29. }
Newbie
 
Join Date: Oct 2009
Location: Denmark
Posts: 5
#6: 3 Weeks Ago

re: Uninstalling a software with C#


and if you need to see some other related functions:

Expand|Select|Wrap|Line Numbers
  1. private ManagementObject GetProduct(string Name)
  2. {
  3.    SelectQuery allProductsQuery = new SelectQuery("Win32_Product");
  4.    ManagementObjectSearcher allProducts =
  5.             new ManagementObjectSearcher(allProductsQuery);
  6.  
  7.    foreach (ManagementObject product in allProducts.Get())
  8.    {
  9.       foreach (PropertyData property in product.Properties)
  10.       {
  11.          if (property.Name == "Name" &&
  12.              property.Value != null &&                   
  13.              property.Value.ToString().Contains(Name))
  14.          {
  15.              return product;
  16.           }
  17.        }
  18.     }
  19.  
  20.     return null;
  21. }
  22.  
  23. private string MakeUninstallString(ManagementObject product)
  24. {
  25.    StringBuilder UninstallString = new StringBuilder();
  26.  
  27.    UninstallString.Append("Win32_Product.");
  28.  
  29.    foreach (PropertyData property in product.Properties)
  30.    {
  31.       if (property.Name == "IdentifyingNumber")
  32.       {
  33.          UninstallString.Append(@"IdentifyingNumber=" + "\"");
  34.          UninstallString.Append(property.Value.ToString());
  35.          UninstallString.Append("\"");
  36.       }
  37.       else if (property.Name == "Name")
  38.       {
  39.          UninstallString.Append(@",Name=" + "\"");
  40.          UninstallString.Append(property.Value.ToString());
  41.          UninstallString.Append("\"");
  42.       }
  43.       else if (property.Name == "Version")
  44.       {
  45.          UninstallString.Append(@",Version=" + "\"");
  46.          UninstallString.Append(property.Value.ToString());
  47.          UninstallString.Append("\"");
  48.       }
  49. }
  50.  
  51. .......
  52. .......
  53.  
  54. ManagementObject Product = GetProduct(ProductName);
  55.  
  56. string UninstallString = MakeUninstallString(Product);
  57.  
  58. Res = Uninstall(UninstallString);
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,743
#7: 3 Weeks Ago

re: Uninstalling a software with C#


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.
Newbie
 
Join Date: Oct 2009
Location: Denmark
Posts: 5
#8: 3 Weeks Ago

re: Uninstalling a software with C#


Aha... that is cool. Thank you!
Reply