Browse Source

Filter process names

master
unknown 12 years ago
parent
commit
854f350c8c

+ 6
- 0
WindowMonitor/App.config View File

@@ -0,0 +1,6 @@
1
+<?xml version="1.0" encoding="utf-8" ?>
2
+<configuration>
3
+  <appSettings>
4
+    <add key="ProcessMatcher" value="^&quot;(.*?)&quot;.*(-jar\s+[^\s]+).*?$|^&quot;(.*?)&quot;"/>
5
+  </appSettings>
6
+</configuration>

+ 4
- 2
WindowMonitor/Program.cs View File

@@ -1,6 +1,7 @@
1 1
 namespace WindowMonitor
2 2
 {
3 3
     using System;
4
+    using System.Configuration;
4 5
     using System.ServiceProcess;
5 6
 
6 7
     /// <summary>
@@ -14,9 +15,10 @@
14 15
         /// </summary>
15 16
         public static void Main()
16 17
         {
18
+            var service = new WindowMonitorService(ConfigurationManager.AppSettings["ProcessMatcher"]);
19
+
17 20
             if (Environment.UserInteractive)
18 21
             {
19
-                var service = new WindowMonitorService();
20 22
                 service.Startup();
21 23
                 Console.WriteLine("Service started; Press <enter> to stop.");
22 24
                 Console.ReadLine();
@@ -26,7 +28,7 @@
26 28
             {
27 29
                 ServiceBase.Run(new ServiceBase[] 
28 30
                 { 
29
-                    new WindowMonitorService() 
31
+                    service
30 32
                 });                
31 33
             }
32 34
         }

+ 6
- 7
WindowMonitor/WindowMonitor.cs View File

@@ -1,7 +1,6 @@
1 1
 namespace WindowMonitor
2 2
 {
3 3
     using System;
4
-    using System.Diagnostics;
5 4
     using System.Management;
6 5
     using System.Runtime.InteropServices;
7 6
     using System.Text;
@@ -55,16 +54,16 @@
55 54
             int processId;
56 55
             GetWindowThreadProcessId(handle, out processId);
57 56
 
57
+            if (processId == 0)
58
+            {
59
+                return string.Empty;
60
+            }
61
+
58 62
             var wmiQuery = string.Format("select CommandLine from Win32_Process where ProcessId={0}", processId);
59 63
             var searcher = new ManagementObjectSearcher(wmiQuery);
60 64
             var resultEnumerator = searcher.Get().GetEnumerator();
61 65
 
62
-            if (resultEnumerator.MoveNext())
63
-            {
64
-                return resultEnumerator.Current["CommandLine"].ToString();
65
-            }
66
-
67
-            return string.Empty;
66
+            return resultEnumerator.MoveNext() ? resultEnumerator.Current["CommandLine"].ToString() : string.Empty;
68 67
         }
69 68
 
70 69
         [DllImport("user32.dll")]

+ 4
- 0
WindowMonitor/WindowMonitor.csproj View File

@@ -71,6 +71,7 @@
71 71
   </PropertyGroup>
72 72
   <ItemGroup>
73 73
     <Reference Include="System" />
74
+    <Reference Include="System.Configuration" />
74 75
     <Reference Include="System.Core" />
75 76
     <Reference Include="System.Management" />
76 77
     <Reference Include="System.Xml.Linq" />
@@ -91,6 +92,9 @@
91 92
     <Compile Include="Program.cs" />
92 93
     <Compile Include="Properties\AssemblyInfo.cs" />
93 94
   </ItemGroup>
95
+  <ItemGroup>
96
+    <None Include="App.config" />
97
+  </ItemGroup>
94 98
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
95 99
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
96 100
        Other similar extension points exist, see Microsoft.Common.targets.

+ 31
- 2
WindowMonitor/WindowMonitorService.cs View File

@@ -1,7 +1,10 @@
1 1
 namespace WindowMonitor
2 2
 {
3 3
     using System;
4
+    using System.Linq;
4 5
     using System.ServiceProcess;
6
+    using System.Text;
7
+    using System.Text.RegularExpressions;
5 8
     using System.Timers;
6 9
 
7 10
     /// <summary>
@@ -19,12 +22,21 @@
19 22
         /// </summary>
20 23
         private WindowMonitor monitor;
21 24
 
25
+        /// <summary>
26
+        /// The regex to use to extract image names.
27
+        /// </summary>
28
+        private Regex filterRegex;
29
+
22 30
         /// <summary>
23 31
         /// Initializes a new instance of the <see cref="WindowMonitorService"/> class. 
24 32
         /// </summary>
25
-        public WindowMonitorService()
33
+        /// <param name="imageFilter">
34
+        /// The filter to use to extract image names.
35
+        /// </param>
36
+        public WindowMonitorService(string imageFilter)
26 37
         {
27 38
             this.InitializeComponent();
39
+            this.filterRegex = new Regex(imageFilter);
28 40
         }
29 41
 
30 42
         /// <summary>
@@ -82,7 +94,24 @@
82 94
         {
83 95
             var handle = this.monitor.GetActiveWindowHandle();
84 96
             Console.WriteLine(this.monitor.GetWindowTitle(handle));
85
-            Console.WriteLine(this.monitor.GetWindowFileName(handle));
97
+
98
+            var fileName = this.monitor.GetWindowFileName(handle);
99
+            Console.WriteLine(fileName);
100
+
101
+            var matches = this.filterRegex.Match(fileName);
102
+            var builder = new StringBuilder();
103
+
104
+            for (var i = 1; i <= matches.Groups.Count; i++)
105
+            {
106
+                if (builder.Length > 0)
107
+                {
108
+                    builder.Append(' ');
109
+                }
110
+
111
+                builder.Append(matches.Groups[i].Value);
112
+            }
113
+
114
+            Console.WriteLine(builder);
86 115
             Console.WriteLine();
87 116
         }
88 117
     }

Loading…
Cancel
Save