Show All Utility and Their Details in WPF

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:

  1. <Window x:Class="List_And_Uninstall_utility.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         Title="MainWindow" Height="470" Width="725" Loaded="Window_Loaded_1">  
  5.     <Grid>  
  6.         <DataGrid HorizontalAlignment="Left" Margin="25,22,0,0" VerticalAlignment="Top" Height="350" Width="680" MouseLeftButtonUp="dgInstalld_MouseLeftButtonUp_1"  
  7.                   RenderTransformOrigin="0.196,0.122" Name="dgInstalld" AutoGenerateColumns="False">  
  8.             <DataGrid.Columns>  
  9.                 <DataGridTextColumn x:Name="cID" Binding="{Binding pName}" Header="Application Name" Width="350" />  
  10.                 <DataGridTextColumn x:Name="cName" Binding="{Binding pKey}" Header="Product Key" />  
  11.             </DataGrid.Columns>  
  12.         </DataGrid>  
  13.     </Grid>  
  14. </Window>  
My XAML.cs code:
  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Configuration.Install;  
  5. using System.Data;  
  6. using System.Linq;  
  7. using System.Runtime.InteropServices;  
  8. using System.Text;  
  9. using System.Threading.Tasks;  
  10. using System.Windows;  
  11. using System.Windows.Controls;  
  12. using System.Windows.Data;  
  13. using System.Windows.Documents;  
  14. using System.Windows.Input;  
  15. using System.Windows.Media;  
  16. using System.Windows.Media.Imaging;  
  17. using System.Windows.Navigation;  
  18. using System.Windows.Shapes;  
  19. using System.Configuration.Install;  
  20.   
  21. namespace List_And_Uninstall_utility  
  22. {  
  23.     /// <summary>  
  24.     /// Interaction logic for MainWindow.xaml  
  25.     /// </summary>  
  26.     public partial class MainWindow : Window  
  27.     {  
  28.   
  29.         string pKeyValue = null;  
  30.         string val = string.Empty;  
  31.         string Key = string.Empty;  
  32.   
  33.         [DllImport("msi.dll", CharSet = CharSet.Unicode)]  
  34.         static extern Int32 MsiGetProductInfo(string product, string property,  
  35.             [Out] StringBuilder valueBuf, ref Int32 len);  
  36.         [DllImport("msi.dll", SetLastError = true)]  
  37.         static extern int MsiEnumProducts(int iProductIndex,  
  38.             StringBuilder lpProductBuf);  
  39.   
  40.   
  41.         public MainWindow()  
  42.         {  
  43.             InitializeComponent();  
  44.         }  
  45.   
  46.         private void Window_Loaded_1(object sender, RoutedEventArgs e)  
  47.         {  
  48.             BindAllUtilities();  
  49.         }  
  50.   
  51.         private void BindAllUtilities()  
  52.         {  
  53.             List<UtilitiesDataInfo> lstDataBind = new List<UtilitiesDataInfo>();  
  54.             StringBuilder sbProductCode = new StringBuilder(39);  
  55.             int iIdx = 0;  
  56.             while (0 == MsiEnumProducts(iIdx++, sbProductCode))  
  57.             {  
  58.                 Int32 productNameLen = 512;  
  59.                 StringBuilder sbProductName = new StringBuilder(productNameLen);  
  60.                 MsiGetProductInfo(sbProductCode.ToString(), "ProductName", sbProductName, ref productNameLen);  
  61.                 lstDataBind.Add(new UtilitiesDataInfo { pKey = sbProductCode.ToString(), pName = sbProductName.ToString() });  
  62.             }  
  63.             dgInstalld.DataContext = lstDataBind;  
  64.             this.dgInstalld.ItemsSource = lstDataBind;  
  65.         }  
  66.   
  67.   
  68.         private void dgInstalld_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)  
  69.         {  
  70.             IList rows = dgInstalld.SelectedItems;  
  71.             Key = ((List_And_Uninstall_utility.UtilitiesDataInfo)(dgInstalld.SelectedItems[0])).pName;  
  72.             val = ((List_And_Uninstall_utility.UtilitiesDataInfo)(dgInstalld.SelectedItems[0])).pKey;  
  73.             System.Windows.MessageBox.Show("Selected Utility: " + System.Environment.NewLine + "Name " + Key + System.Environment.NewLine + "Value " + val);  
  74.         }  
  75.     }  
  76.   
  77.     public class UtilitiesDataInfo  
  78.     {  
  79.         public string pName { getset; }  
  80.         public string pKey { getset; }  
  81.     }  
  82. }  
Now run the application:

list of utilities
                                                                              Image 1

Now click on any record:

selected utility
                                                                           Image 2