Here I am using a WPF application and binding all utilities in a Data Grid. If you click on any record then I am showing the selected record information in a message box.
The following is my XAML code:
- <Window x:Class="List_And_Uninstall_utility.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="470" Width="725" Loaded="Window_Loaded_1">
- <Grid>
- <DataGrid HorizontalAlignment="Left" Margin="25,22,0,0" VerticalAlignment="Top" Height="350" Width="680" MouseLeftButtonUp="dgInstalld_MouseLeftButtonUp_1"
- RenderTransformOrigin="0.196,0.122" Name="dgInstalld" AutoGenerateColumns="False">
- <DataGrid.Columns>
- <DataGridTextColumn x:Name="cID" Binding="{Binding pName}" Header="Application Name" Width="350" />
- <DataGridTextColumn x:Name="cName" Binding="{Binding pKey}" Header="Product Key" />
- </DataGrid.Columns>
- </DataGrid>
- </Grid>
- </Window>
My XAML.cs code:
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Configuration.Install;
- using System.Data;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- using System.Configuration.Install;
-
- namespace List_And_Uninstall_utility
- {
-
-
-
- public partial class MainWindow : Window
- {
-
- string pKeyValue = null;
- string val = string.Empty;
- string Key = string.Empty;
-
- [DllImport("msi.dll", CharSet = CharSet.Unicode)]
- static extern Int32 MsiGetProductInfo(string product, string property,
- [Out] StringBuilder valueBuf, ref Int32 len);
- [DllImport("msi.dll", SetLastError = true)]
- static extern int MsiEnumProducts(int iProductIndex,
- StringBuilder lpProductBuf);
-
-
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void Window_Loaded_1(object sender, RoutedEventArgs e)
- {
- BindAllUtilities();
- }
-
- private void BindAllUtilities()
- {
- List<UtilitiesDataInfo> lstDataBind = new List<UtilitiesDataInfo>();
- StringBuilder sbProductCode = new StringBuilder(39);
- int iIdx = 0;
- while (0 == MsiEnumProducts(iIdx++, sbProductCode))
- {
- Int32 productNameLen = 512;
- StringBuilder sbProductName = new StringBuilder(productNameLen);
- MsiGetProductInfo(sbProductCode.ToString(), "ProductName", sbProductName, ref productNameLen);
- lstDataBind.Add(new UtilitiesDataInfo { pKey = sbProductCode.ToString(), pName = sbProductName.ToString() });
- }
- dgInstalld.DataContext = lstDataBind;
- this.dgInstalld.ItemsSource = lstDataBind;
- }
-
-
- private void dgInstalld_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
- {
- IList rows = dgInstalld.SelectedItems;
- Key = ((List_And_Uninstall_utility.UtilitiesDataInfo)(dgInstalld.SelectedItems[0])).pName;
- val = ((List_And_Uninstall_utility.UtilitiesDataInfo)(dgInstalld.SelectedItems[0])).pKey;
- System.Windows.MessageBox.Show("Selected Utility: " + System.Environment.NewLine + "Name " + Key + System.Environment.NewLine + "Value " + val);
- }
- }
-
- public class UtilitiesDataInfo
- {
- public string pName { get; set; }
- public string pKey { get; set; }
- }
- }
Now run the application:
Image 1
Now click on any record:
Image 2