Created
June 8, 2015 03:09
-
-
Save RhysC/212eae68020fa69d2ce2 to your computer and use it in GitHub Desktop.
Get all installed applications and service on local machine (linqpad script)
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
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\System.ServiceProcess.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Configuration.Install.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Windows.Forms.dll</Reference> | |
<Namespace>System.ServiceProcess</Namespace> | |
<Namespace>Microsoft.Win32</Namespace> | |
</Query> | |
void Main() | |
{ | |
DisplayAllInstalledApplications(); | |
DisplayAllServices(); | |
} | |
public void DisplayAllInstalledApplications() | |
{ | |
var hives = new []{ | |
RegistryHive.LocalMachine, | |
RegistryHive.CurrentUser | |
}; | |
var keys =new []{ | |
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", | |
@"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" | |
}; | |
var x = from hive in hives | |
from key in keys | |
from app in GetInstalledApps(System.Environment.MachineName, hive, key) | |
select new { | |
Hive = hive, | |
Key = key, | |
App = app | |
}; | |
x.GroupBy (g => g.App) | |
.Select (g => new {Application = g.Key, UnInstallLocations = g.Select (h => new { h.Hive, RegKey = h.Key} )}) | |
.OrderBy (g => g.Application) | |
.Dump("All Installed Applications"); | |
} | |
public void DisplayAllServices() | |
{ | |
//https://msdn.microsoft.com/en-us/library/hde9d63a%28v=vs.110%29.aspx | |
ServiceController.GetServices() | |
//.Where (sc => sc.Status == ServiceControllerStatus.Running) | |
.Select (sc => new { | |
sc.DisplayName, | |
sc.ServiceName, | |
sc.Status, | |
sc.ServiceType, | |
Dependendencies= new Lazy<Dictionary<string,ServiceController[]>>(()=> new Dictionary<string,ServiceController[]> {{"DependentServices", sc.DependentServices},{"ServicesDependedOn", sc.ServicesDependedOn}}), | |
Abilities = new Lazy<Dictionary<string,bool>>(()=> new Dictionary<string,bool>{{"CanPauseAndContinue",sc.CanPauseAndContinue},{"CanShutdown", sc.CanShutdown},{"CanStop",sc.CanStop}}) | |
}) | |
.OrderByDescending (sc => sc.Status) | |
.ThenBy (sc => sc.DisplayName) | |
.Dump("All Services"); | |
} | |
public IEnumerable<string> GetInstalledApps(string p_machineName, RegistryHive p_hive, string p_subKeyName) | |
{ | |
using (RegistryKey regHive = RegistryKey.OpenRemoteBaseKey(p_hive, p_machineName)) | |
{ | |
using (RegistryKey regKey = regHive.OpenSubKey(p_subKeyName)) | |
{ | |
if (regKey != null) | |
{ | |
foreach (string kn in regKey.GetSubKeyNames()) | |
{ | |
using (var subkey = regKey.OpenSubKey(kn)) | |
{ | |
var value = subkey.GetValue("DisplayName") as string; | |
if(value != null) | |
{ | |
yield return value; | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment