I am currently working on a C# project. I am using the ManagementScope with the Win32_Product. I am successfully retrieving the software name, vendor and the install location but I want to be able to get either the path to the actual executable to launch the software.
I am using the following code to retrieve the information.
try
{
ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Product");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection queryCollection = searcher.Get();
DataSet ds = new DataSet();
dt = new DataTable();
ds.Tables.Add(dt);
DataColumn sofNameCol = new DataColumn("Software", typeof(string));
DataColumn sofPublish = new DataColumn("Publisher", typeof(string));
DataColumn sofInstallCol = new DataColumn("Install Path", typeof(string));
dt.Columns.Add(sofNameCol);
dt.Columns.Add(sofPublish);
dt.Columns.Add(sofInstallCol);
foreach (ManagementObject m in queryCollection)
{
string softwareName = m["Name"].ToString();
string publisher = m["Vendor"].ToString();
object install = m["InstallLocation"];
if (install != null)
{
DataRow rw = dt.NewRow();
开发者_运维问答 dt.Rows.Add(rw);
rw["Software"] = softwareName;
rw["Publisher"] = publisher;
rw["Install Path"] = install;
}
Console.WriteLine("Software Name: {0}", m["Name"]);
Console.WriteLine("Publisher: {0}", m["Vendor"]);
Console.WriteLine("Path: {0}", m["InstallLocation"]);
}
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new System.Windows.Threading.DispatcherOperationCallback(delegate
{
tblSoftware.ItemsSource = ds.Tables[0].DefaultView;
sortData();
return null;
}), null);
}
catch (ManagementException ex)
{
Console.WriteLine("Management Error: " + ex.Message);
}
I have tried google searches on how I can expand this code to retrieve either the executable path. Thanks for any help you can provide.
精彩评论