-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemProfiler.cs
167 lines (153 loc) · 6.31 KB
/
SystemProfiler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
namespace FormulatedAutomation.UiPathProfiler.Activities
{
public class SystemProfiler
{
public class SystemSnapshot
{
public DateTimeOffset CreatedAt { get; set; }
public string WindowsVersion { get; set; }
public List<InstalledProgram> InstalledPrograms { get; set; }
public List<ProcessDetail> ProcessDetails { get; set; }
public string RunUUID { get; set; }
}
public class InstalledProgram
{
public string Name { get; set; }
public string InstallDate { get; set; }
public string Version { get; set; }
public string Source { get; set; }
}
public class ProcessDetail
{
public string ProcessName { get; set; }
public int Id { get; internal set; }
public string ExecutablePath { get; internal set; }
public string CommandLine { get; internal set; }
public string CreationDate { get; internal set; }
public int ParentProcessId { get; internal set; }
public int WorkingSetSize { get; internal set; }
}
public SystemProfiler()
{
}
public List<InstalledProgram> GetInstalledApps()
{
String key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
String wow64key = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall";
List<InstalledProgram> progList = FromUninstallRegisty("hklm", key);
progList.AddRange(FromUninstallRegisty("hklm", wow64key));
progList.AddRange(FromUninstallRegisty("hkcu", key));
progList.Sort((a, b) => a.Name.CompareTo(b.Name));
return progList;
}
private List<InstalledProgram> FromUninstallRegisty(string root, string key)
{
RegistryKey rk;
List<InstalledProgram> progList = new List<InstalledProgram>();
if (root == "hklm")
{
rk = Registry.LocalMachine.OpenSubKey(key);
}
else
{
rk = Registry.CurrentUser.OpenSubKey(key);
}
foreach (string skName in rk.GetSubKeyNames())
{
using (RegistryKey sk = rk.OpenSubKey(skName))
{
try
{
progList.Add(new InstalledProgram
{
Name = RegistryValue(sk, "DisplayName"),
Version = RegistryValue(sk, "DisplayVersion"),
InstallDate = RegistryValue(sk, "InstallDate"),
Source = "Registry - " + root + ":" + key,
});
}
catch (Exception ex)
{
// Ignore errors accessing the registry, usually due to security constraints
Debug.WriteLine("Exception: {0}", ex.Message);
}
}
}
List<InstalledProgram> cleanedProgList = progList.FindAll(p => p.Name.Length > 0);
return cleanedProgList;
}
public List<ProcessDetail> GetRunningProcesses()
{
List<ProcessDetail> procList = new List<ProcessDetail>();
var wmiQueryString = "SELECT ProcessId, Name, ExecutablePath, CommandLine, CreationDate, ParentProcessId, WorkingSetSize FROM Win32_Process";
//var wmiQueryString = "SELECT * FROM Win32_Process";
using (var searcher = new ManagementObjectSearcher(wmiQueryString))
using (var results = searcher.Get())
{
var query = from p in Process.GetProcesses()
join mo in results.Cast<ManagementObject>()
on p.Id equals (int)(uint)mo["ProcessId"]
select new
{
Process = p,
Id = (int)(uint)mo["ProcessId"],
Path = (string)mo["ExecutablePath"],
Name = (string)mo["Name"],
CommandLine = (string)mo["CommandLine"],
CreationDate = (string)mo["CreationDate"],
ParentProcessId = (int)(uint)mo["ParentProcessId"],
WorkingSetSize = (int)(UInt64)mo["WorkingSetSize"],
};
foreach (var item in query)
{
Process process = item.Process;
try
{
ProcessDetail processDetail = new ProcessDetail
{
Id = item.Id,
ParentProcessId = item.ParentProcessId,
ProcessName = item.Name,
ExecutablePath = item.Path,
CommandLine = item.CommandLine,
CreationDate = item.CreationDate,
WorkingSetSize = item.WorkingSetSize,
};
procList.Add(processDetail);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(process.Id);
}
}
}
procList.Sort((a, b) => a.ProcessName.CompareTo(b.ProcessName));
return procList;
}
public SystemSnapshot GetInitialProfile()
{
SystemSnapshot systemReport = new SystemSnapshot
{
InstalledPrograms = GetInstalledApps(),
ProcessDetails = GetRunningProcesses()
};
return systemReport;
}
private string RegistryValue(RegistryKey rk, string key)
{
string val = rk.GetValue(key) as string;
if (String.IsNullOrEmpty(val))
{
return "";
}
return val;
}
}
}