Hi Adam,
"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:uV**************@TK2MSFTNGP12.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.Management;" somewhere
...
static void ListAllProducts()
{
SelectQuery allProductsQuery = new SelectQuery("Win32_Product");
ManagementObjectSearcher allProducts =
new ManagementObjectSearcher(allProductsQuery);
foreach(ManagementObject product in allProducts.Get())
{
foreach(PropertyData property in product.Properties)
{
Console.WriteLine("{0} = {1}", property.Name,
property.Value);
}
Console.WriteLine("Path = {0}", product.Path);
Console.WriteLine();
}
}
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 UninstallProduct(string path)
{
ManagementObject product = new ManagementObject(path);
if ((product != null) && (product.Path.ClassName ==
"Win32_Product"))
{
object result = product.InvokeMethod("Uninstall", null);
Console.WriteLine("The Uninstall method result is {0}",
result.ToString());
}
}
A call to the UninstallProduct method above might look like this:
UninstallProduct(
"Win32_Product" +
".IdentifyingNumber=\"{F934F310-BFEB-40CA-801B-CC7136CEE35F}\""
+
",Name=\"ObjectSpaces Mapper Utility\"" +
",Version=\"1.0.0\"");
I split the path over several lines for readability. FYI, the
IdentifyingNumber 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