public static string GetInstalledApps()
{
//Declare the string to hold the list:
string Application = null;
//The registry key:
string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
{
//Let's go through the registry keys and get the info we need:
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
//If the key has value, continue, if not, skip it:
if (!(sk.GetValue("DisplayName") == null))
{
//get the version string
if (sk.GetValue("InstallLocation") == null)
Application += sk.GetValue("DisplayName") + " - Install path not known\n"; //Nope, not here.
else
Application += sk.GetValue("DisplayName") + " - " + sk.GetValue("InstallLocation") + "\n"; //Yes, here it is...
}
}
catch (Exception ex)
{
//exception
}
}
}
}
return Application;
}
///this is to get the current version of msi file
public static int GetCurrVersions(string inputFile)
{
// Get the type of the Windows Installer object
Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
// Create the Windows Installer object
Installer installer = (Installer)Activator.CreateInstance(installerType);
// Open the MSI database in the input file
Database database = installer.OpenDatabase(inputFile, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
// Open a view on the Property table for the version property
View dataView = database.OpenView("SELECT * FROM Property WHERE Property = 'ProductVersion'");
// Execute the view query
dataView.Execute(null);
// Get the record from the view
Record dataRecord = dataView.Fetch();
// Get the version from the data
string version = dataRecord.get_StringData(2);
//close the view
dataView.Close();
}
Query LINQ
static void DisplayInstalledApplications()
{
string registryKey =
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
using (Microsoft.Win32.RegistryKey key =
Registry.LocalMachine.OpenSubKey(registryKey))
{
var query = from a in
key.GetSubKeyNames()
let r = key.OpenSubKey(a)
select new
{
Application = r.GetValue("DisplayName")
};
foreach (var item in query)
{
if (item.Application != null)
Console.WriteLine(item.Application);
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment