Check out WMI Code Creator (a free tool from Microsoft) - it can generate WMI code for you in a variety of languages, including C #.
Here is an example illustrating the use of the Win32_Product.Uninstall method. You need to know the GUID, name and version of the application that you want to remove, since they are the key properties of the Win32_Product class:
... ManagementObject app = new ManagementObject(scope, "Win32_Product.IdentifyingNumber='{99052DB7-9592-4522-A558-5417BBAD48EE}',Name='Microsoft ActiveSync',Version='4.5.5096.0'", null); ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null); Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]);
If you have partial information about the application (for example, only name or name and version), you can use the SELECT query to get the corresponding Win32_Process object:
... SelectQuery query = new SelectQuery("Win32_Product", "Name='Microsoft ActiveSync'"); EnumerationOptions enumOptions = new EnumerationOptions(); enumOptions.ReturnImmediately = true; enumOptions.Rewindable = false; ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, options); foreach (ManagementObject app in searcher.Get()) { ManagementBaseObject outParams = app.InvokeMethod("Uninstall", null); Console.WriteLine("The Uninstall method result: {0}", outParams["ReturnValue"]); }
Helen source share