看一下(来自 Microsoft 的免费工具)— 它可以为您生成各种语言的 WMI 代码,包括 C#。
这是一个示例,说明了Win32_Product.Uninstall
方法的使用。您需要知道要卸载的应用程序的 GUID、名称和版本,因为它们是应用程序的关键属性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"]);
如果您有有关应用程序的部分信息(例如只有名称或名称和版本),您可以使用SELECT
查询得到对应的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"]);
}