ManagementScope和Win32_Product检索可执行路径

时间:2011-06-05 20:34:46

标签: c#

我目前正在开发一个C#项目。我正在使用ManagementScope和Win32_Product。我正在成功检索软件名称,供应商和安装位置,但我希望能够获得实际可执行文件的路径以启动软件。

我使用以下代码检索信息。

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);
            }

我试过谷歌搜索如何扩展此代码以检索可执行路径。感谢您的任何帮助,您可以提供。

0 个答案:

没有答案