Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Get List Of All Utility in WPF C-Sharp .NET

Article:
Viewed:  1373 
Posted On:  20/02/2014 09:33:00 
How we can get list of all installed utility in your machine in WPF ? 

In this article I am going to show how we can show all utility lists in WPF, C# application.

Here I am using WPF application and I am binding all utilities in a Data Grid. If you click on any record then I am showing selecting record info in a message box.

Below 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="MyDataGrid_MouseLeftButtonUp_1"
                  RenderTransformOrigin="0.196,0.122" Name="MyDataGrid" 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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    
    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,
            [OutStringBuilder 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() });
            }
            MyDataGrid.DataContext = lstDataBind;
            this.MyDataGrid.ItemsSource = lstDataBind;
        }
 
 
        private void MyDataGrid_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 { getset; }
        public string pKey { getset; }
    }
}
 
Now Run the application:

Utilities in WPF 1.png

Image 1.

Now Click on any record:

Utilities in WPF 2.png

Image 2.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us