-
-
Save andySF/19f3569b50db18095a3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using IONClient.Config; | |
using IONClient.Logs; | |
using IONClient.Remote; | |
using IONClient.Remote.Models; | |
using Microsoft.Win32; | |
namespace IONClient.Local.Collector | |
{ | |
public class PopulateInstalledPrograms | |
{ | |
[DllImport("shell32.dll")] | |
private static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, StringBuilder lpIconPath, | |
out ushort lpiIcon); | |
[DllImport("shell32.dll")] | |
private static extern IntPtr ExtractIcon(IntPtr hInst, string lpszExeFileName, int nIconIndex); | |
public static List<InstalledProgram> Collect() | |
{ | |
try | |
{ | |
List<InstalledProgram> installedPrograms = new List<InstalledProgram>(); | |
//1. SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall inside CurrentUser | |
//2. SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall inside LocalMachine | |
//3. SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall in LocalMachine | |
//To determine if its an update, there will be a key called IsMinorUpgrade. | |
//This is present and set to a 1 for updates. If it's 0 or not present, then it's not an update. | |
string windowsCurrentversionUninstall = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; | |
string wow6432NodeMicrosoftWindowsCurrentversionUninstall = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; | |
try | |
{ | |
using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(windowsCurrentversionUninstall)) | |
{ | |
ProcessRegistries(registryKey, installedPrograms); | |
} | |
} | |
catch { } | |
try | |
{ | |
using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(windowsCurrentversionUninstall)) | |
{ | |
ProcessRegistries(registryKey, installedPrograms); | |
} | |
} | |
catch { } | |
try | |
{ | |
using ( | |
RegistryKey registryKey = | |
Registry.LocalMachine.OpenSubKey(wow6432NodeMicrosoftWindowsCurrentversionUninstall)) | |
{ | |
ProcessRegistries(registryKey, installedPrograms); | |
} | |
} | |
catch | |
{ | |
} | |
return installedPrograms; | |
} | |
catch(Exception ex) | |
{ | |
Create.Log("Collect installed programs faild with error: " + ex.Message, EventLogEntryType.Error); | |
return null; | |
} | |
} | |
private static void ProcessRegistries(RegistryKey registryKey, List<InstalledProgram> installedPrograms) | |
{ | |
try | |
{ | |
if (registryKey != null) | |
{ | |
foreach (string subkeyName in registryKey.GetSubKeyNames()) | |
{ | |
InstalledProgram installedProgram = new InstalledProgram(); | |
using (RegistryKey subKey = registryKey.OpenSubKey(subkeyName)) | |
{ | |
installedProgram.DisplayName = subKey.GetValue("DisplayName") != null | |
? subKey.GetValue("DisplayName").ToString() | |
: String.Empty; | |
installedProgram.Publisher = subKey.GetValue("Publisher") != null | |
? subKey.GetValue("Publisher").ToString() | |
: String.Empty; | |
installedProgram.InstallDate = subKey.GetValue("InstallDate") != null | |
? subKey.GetValue("InstallDate").ToString() | |
: String.Empty; | |
installedProgram.Version = subKey.GetValue("Version") != null | |
? subKey.GetValue("Version").ToString() | |
: String.Empty; | |
installedProgram.UninstallString = subKey.GetValue("UninstallString") != null | |
? subKey.GetValue("UninstallString").ToString() | |
: String.Empty; | |
installedProgram.Size = subKey.GetValue("Size") != null | |
? subKey.GetValue("Size").ToString() | |
: String.Empty; | |
installedProgram.Installed = true; | |
installedProgram.MachineId = Variables.MachineId; | |
String displayIconPath = subKey.GetValue("DisplayIcon") != null | |
? subKey.GetValue("DisplayIcon").ToString() | |
: String.Empty; | |
installedProgram.Icon = GetIconToBytes(subKey); | |
} | |
if (installedProgram.DisplayName != String.Empty) | |
if (!ProgramExists(installedPrograms, installedProgram)) | |
installedPrograms.Add(installedProgram); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw new Exception("ProcessRegistries: " + ex.Message); | |
} | |
} | |
private static bool ProgramExists(IEnumerable<InstalledProgram> programs, InstalledProgram program) | |
{ | |
return programs.Any(p => p.DisplayName == program.DisplayName); | |
} | |
private static byte[] GetIconToBytes(RegistryKey subkey) | |
{ | |
String DisplayIcon = subkey.GetValue("DisplayIcon") != null | |
? subkey.GetValue("DisplayIcon").ToString() | |
: String.Empty; | |
if (!String.IsNullOrEmpty(DisplayIcon)) | |
{ | |
if (DisplayIcon.Contains(",")) | |
{ | |
var displayIconSplited = DisplayIcon.Split(','); | |
if (File.Exists(displayIconSplited[0])) | |
{ | |
//return IconToBytes(IconFromFilePath(displayIconSplited[0])); | |
ushort uicon; | |
StringBuilder strB = new StringBuilder(displayIconSplited[0]); | |
IntPtr handle = ExtractAssociatedIcon(IntPtr.Zero, strB, out uicon); | |
Icon ico = Icon.FromHandle(handle); | |
return ImageToByte(ico.ToBitmap()); | |
} | |
} | |
if (File.Exists(DisplayIcon)) | |
{ | |
if (Path.GetExtension(DisplayIcon) == ".exe") | |
{ | |
return ImageToByte(Icon.ExtractAssociatedIcon(DisplayIcon).ToBitmap()); | |
} | |
if (Path.GetExtension(DisplayIcon) == ".ico") | |
{ | |
Icon c = new Icon(DisplayIcon); | |
//icon.Save(new FileStream(@"c:\ico"+DateTime.Now.ToFileTimeUtc().ToString()+".ico" , FileMode.Create)); | |
return ImageToByte(c.ToBitmap()); | |
} | |
} | |
String InstallLocation = subkey.GetValue("InstallLocation") != null | |
? subkey.GetValue("InstallLocation").ToString() | |
: String.Empty; | |
if (!String.IsNullOrEmpty(InstallLocation)) | |
{ | |
if (File.Exists(InstallLocation + DisplayIcon)) | |
{ | |
if (Path.GetExtension(InstallLocation + DisplayIcon) == ".exe") | |
{ | |
return ImageToByte(Icon.ExtractAssociatedIcon(InstallLocation + DisplayIcon).ToBitmap()); | |
} | |
if (Path.GetExtension(InstallLocation + DisplayIcon) == ".ico") | |
{ | |
Icon c = new Icon(InstallLocation + DisplayIcon); | |
return ImageToByte(c.ToBitmap()); | |
} | |
} | |
} | |
} | |
//TODO: Get Icons to Bytes from diffrent scenarious | |
//http://stackoverflow.com/questions/12283405/get-displayicon-for-installed-programs | |
//if string is null or empty then find icon by searching install dir or smth | |
else | |
{ | |
} | |
return null; | |
} | |
private static Icon IconFromFilePath(string filePath) | |
{ | |
Icon result = null; | |
try | |
{ | |
result = Icon.ExtractAssociatedIcon(filePath); | |
} | |
catch { } | |
return result; | |
} | |
private static byte[] IconToBytes(Icon icon) | |
{ | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
icon.Save(ms); | |
return ms.ToArray(); | |
} | |
} | |
private static Icon BytesToIcon(byte[] bytes) | |
{ | |
using (MemoryStream ms = new MemoryStream(bytes)) | |
{ | |
return new Icon(ms); | |
} | |
} | |
private static byte[] ImageToByte(Image img) | |
{ | |
byte[] byteArray = new byte[0]; | |
using (MemoryStream stream = new MemoryStream()) | |
{ | |
img.Save(stream, ImageFormat.Png); | |
stream.Close(); | |
byteArray = stream.ToArray(); | |
} | |
return byteArray; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment