Elencare i programmi installatti in C#

Reperire l’elenco degli applicativi installati sul pc utilizzando una Windows Form e C# è un’opeazione molto banale, bastano infatti queste poche righe…
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace ApplicationList
{  
   public partial class frmShowList : Form  
   {     
      public frmShowList()    
      {    
         InitializeComponent();     
      }
 
      private void frmShowList _Load(object sender, EventArgs e)     
      {        
         DataTable dtSoftware = new DataTable();        
         dtSoftware.Columns.Add(new DataColumn("Name"));        
         dtSoftware.Columns.Add(new DataColumn("Path"));
        
         foreach (Software software in GetSoftwareInstalled())        
         {           
            DataRow newRow=dtSoftware.NewRow();           
            newRow["Name"] = software.Name;           
            newRow["Path"] = software.Path;           
            dtSoftware.Rows.Add(newRow);        
         }
 
         this.dgPrograms.DataSource = dtSoftware;             
      }
 
      private SoftwareList GetSoftwareInstalled()     
      {        
         SoftwareList sList = new SoftwareList();
 
         String @Key  =@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
      
         using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(Key))
         {           
            foreach (String SubKeys in Key.GetSubKeyNames())           
            {              
               using (RegistryKey SubKey = Key.OpenSubKey(SubKeys))             
               {               
                  if(SubKey.GetValue("DisplayName") != null)         
                  {                
                     Software software = new Software();              
                     software.Name = SubKey.GetValue("DisplayName").ToString();               
                     software.Path = (SubKey.GetValue("InstallLocation") != null) ? SubKey.GetValue("InstallLocation").ToString() : "N.D."; sList.Add(software);                 
                  }            
               }          
            }        
         }
 
         return sList;
      }
 
      public class Software      
      {        
         public String Name { get; set; }        
         public String Path { get; set; }     
      }
      
      public class SoftwareList  : List<Software> 
      {     
      }
   }
}
 

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *