Hi Adam,
"Adam Clauss" <ca*****@tamu.e du> wrote in message
news:uV******** ******@TK2MSFTN GP12.phx.gbl...
I am needing to automate some installation tasks. I first need to
determine if there is already a version of a program installed.
<snip>
There are a lot of ways you can accomplish this, including using the
Microsoft Windows Installer Object Library (COM) or WMI. Since there are
already classes in .NET for WMI, I'll use that as an example. The
Win32_Product WMI class represents products installed on the machine. The
Win32_Product class has the product name, code, description, installed
location, etc., as well as methods to install, uninstall, advertise, etc the
product.
The first step is to determine the WMI "path" to the product you want to
uninstall. This routine will print out all the properties and the WMI path
for all installed products:
// need a "using System.Manageme nt;" somewhere
...
static void ListAllProducts ()
{
SelectQuery allProductsQuer y = new SelectQuery("Wi n32_Product");
ManagementObjec tSearcher allProducts =
new ManagementObjec tSearcher(allPr oductsQuery);
foreach(Managem entObject product in allProducts.Get ())
{
foreach(Propert yData property in product.Propert ies)
{
Console.WriteLi ne("{0} = {1}", property.Name,
property.Value) ;
}
Console.WriteLi ne("Path = {0}", product.Path);
Console.WriteLi ne();
}
}
Once you have the path for the product you wish to uninstall, you can
uninstall with a method that might look like this:
static void UninstallProduc t(string path)
{
ManagementObjec t product = new ManagementObjec t(path);
if ((product != null) && (product.Path.C lassName ==
"Win32_Product" ))
{
object result = product.InvokeM ethod("Uninstal l", null);
Console.WriteLi ne("The Uninstall method result is {0}",
result.ToString ());
}
}
A call to the UninstallProduc t method above might look like this:
UninstallProduc t(
"Win32_Prod uct" +
".IdentifyingNu mber=\"{F934F31 0-BFEB-40CA-801B-CC7136CEE35F}\" "
+
",Name=\"Object Spaces Mapper Utility\"" +
",Version=\"1.0 .0\"");
I split the path over several lines for readability. FYI, the
IdentifyingNumb er property is the "Product Code". Also, the part of the path
that precedes the class name (something like "\\MachineName\ root\cimv2:") is
unnecessary for anything on the local machine, so I omitted it.
Hope that helps.
Regards,
Daniel