-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
197 lines (162 loc) · 7.48 KB
/
MainWindow.xaml.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace NewProcessMonitoring
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private int _watchProcessTimeoutMs = 5000;
private ObservableCollection<ProcessTableItem> _tableItems = new ObservableCollection<ProcessTableItem>();
private ProcessFileWorker _processFileWorker = new ProcessFileWorker(System.IO.Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NewPm", "processes.json"));
private SemaphoreSlim _tableItemsRefreshSemaphore = new SemaphoreSlim(1);
private MinimizeToTray _minimizeToTray;
ManagementObjectSearcher processSearcher = new ManagementObjectSearcher("SELECT ProcessId, Caption, ExecutablePath, CommandLine FROM Win32_Process");
public MainWindow()
{
InitializeComponent();
_minimizeToTray = new MinimizeToTray(this);
var tbnOpenGoogle = new FrameworkElementFactory(typeof(Image));
var tbnOpenFolder = new FrameworkElementFactory(typeof(Image));
BitmapImage tbnOpenGoogleImage = new BitmapImage(new Uri("pack://application:,,,/Assets/WebBrowser.png"));
tbnOpenGoogle.SetValue(Image.SourceProperty, tbnOpenGoogleImage);
BitmapImage tbnOpenFolderImage = new BitmapImage(new Uri("pack://application:,,,/Assets/FolderOpened.png"));
tbnOpenFolder.SetValue(Image.SourceProperty, tbnOpenFolderImage);
tbnOpenGoogle.AddHandler(Image.MouseUpEvent, new MouseButtonEventHandler((s, e) =>
{
if (tProcesses.SelectedItem != null)
{
var path = ((ProcessTableItem)tProcesses.SelectedItem).FullPath;
var fileName = System.IO.Path.GetFileName(path);
System.Diagnostics.Process.Start("https://www.google.com/search?q=What+is+" + fileName);
}
}));
tbnOpenFolder.AddHandler(Image.MouseUpEvent, new MouseButtonEventHandler((s, e) =>
{
if (tProcesses.SelectedItem != null)
{
var filePath = ((ProcessTableItem)tProcesses.SelectedItem).FullPath;
Process.Start("explorer.exe", "/select, \"" + filePath + "\"");
}
}));
this.tProcesses.Columns.Insert(0, new DataGridTemplateColumn()
{
Header = "",
CellTemplate = new DataTemplate() { VisualTree = tbnOpenGoogle }
}
);
this.tProcesses.Columns.Insert(0, new DataGridTemplateColumn()
{
Header = "",
CellTemplate = new DataTemplate() { VisualTree = tbnOpenFolder }
}
);
tProcesses.ItemsSource = _tableItems;
_ = ProcessWatcherLoopAsync();
Application.Current.Exit += App_Exit;
}
public async Task ProcessWatcherLoopAsync()
{
var newTableItems = new List<ProcessTableItem>();
while (true)
{
await Task.Delay(_watchProcessTimeoutMs);
if (!this.IsActive) //watch only if background
await RefreshProcessTableAsync();
}
}
private async Task RefreshProcessTableAsync()
{
await _tableItemsRefreshSemaphore.WaitAsync();
Debug.WriteLine("Refresh table");
var newTableItems = new Dictionary<string, ProcessTableItem>();
using (var results = processSearcher.Get())
{
foreach (var resultItem in results)
{
var mo = (ManagementObject)resultItem;
var pTitle = (string)mo["Caption"];
var pPath = (string)mo["ExecutablePath"];
var pCommandLine = (string)mo["CommandLine"];
if (!string.IsNullOrEmpty(pPath))
{
if (!_processFileWorker.ItemsByFullPath.ContainsKey(pPath))
newTableItems[pPath] = new ProcessTableItem(pTitle, pPath, DateTime.Now);
}
}
}
//Fill table with untrusted items
_tableItems.Clear();
foreach(var newItem in newTableItems.Values)
_tableItems.Add(newItem);
foreach (var untrustedItem in _processFileWorker.ItemsByFullPath.Values.Where(x => !x.IsTrusted))
_tableItems.Add(new ProcessTableItem(untrustedItem.Title, untrustedItem.FullPath, untrustedItem.FirstFoundUtcDate));
//Add new untrusted items to file
_processFileWorker.TryAddNewProcess(newTableItems.Values.Select(x => new ProcessFileItem(x.Name, x.FullPath, x.FirstFoundLocalDate.ToUniversalTime(), false)));
//Order by alph:
var ordered = _tableItems.OrderBy(x => x.Name).ToList();
_tableItems.Clear();
foreach (var item in ordered)
_tableItems.Add(item);
_tableItemsRefreshSemaphore.Release();
}
private void bnApplyAllToTrusted_Click(object sender, RoutedEventArgs e)
{
_tableItemsRefreshSemaphore.Wait();
_processFileWorker.TryAddNewProcess(_tableItems.Select(x => new ProcessFileItem(x.Name, x.FullPath, x.FirstFoundLocalDate.ToUniversalTime(), true)));
_tableItemsRefreshSemaphore.Release();
_ = RefreshProcessTableAsync();
}
//private void bnClose_Click(object sender, RoutedEventArgs e)
//{
// this.Close();
//}
private void bnClearTrustList_Click(object sender, RoutedEventArgs e)
{
_processFileWorker.ClearAllAddedProcesses();
_ = RefreshProcessTableAsync();
}
private void wMain_Activated(object sender, EventArgs e)
{
_ = RefreshProcessTableAsync(); //Force refresh
}
private void tProcesses_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyType == typeof(System.DateTime))
(e.Column as DataGridTextColumn).Binding.StringFormat = "dd.MM.yyyy HH:mm";
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.WindowState = WindowState.Minimized;
e.Cancel = true;
}
private void Window_SourceInitialized(object sender, EventArgs e)
{
WindowExtensions.HideMinimizeAndMaximizeButtons(this);
this.WindowState = WindowState.Minimized;
}
private void App_Exit(object sender, ExitEventArgs e)
{
_minimizeToTray.Dispose();
}
}
}