Browse Source

Filter process names

master
unknown 12 years ago
parent
commit
854f350c8c

+ 6
- 0
WindowMonitor/App.config View File

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
 namespace WindowMonitor
1
 namespace WindowMonitor
2
 {
2
 {
3
     using System;
3
     using System;
4
+    using System.Configuration;
4
     using System.ServiceProcess;
5
     using System.ServiceProcess;
5
 
6
 
6
     /// <summary>
7
     /// <summary>
14
         /// </summary>
15
         /// </summary>
15
         public static void Main()
16
         public static void Main()
16
         {
17
         {
18
+            var service = new WindowMonitorService(ConfigurationManager.AppSettings["ProcessMatcher"]);
19
+
17
             if (Environment.UserInteractive)
20
             if (Environment.UserInteractive)
18
             {
21
             {
19
-                var service = new WindowMonitorService();
20
                 service.Startup();
22
                 service.Startup();
21
                 Console.WriteLine("Service started; Press <enter> to stop.");
23
                 Console.WriteLine("Service started; Press <enter> to stop.");
22
                 Console.ReadLine();
24
                 Console.ReadLine();
26
             {
28
             {
27
                 ServiceBase.Run(new ServiceBase[] 
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
 namespace WindowMonitor
1
 namespace WindowMonitor
2
 {
2
 {
3
     using System;
3
     using System;
4
-    using System.Diagnostics;
5
     using System.Management;
4
     using System.Management;
6
     using System.Runtime.InteropServices;
5
     using System.Runtime.InteropServices;
7
     using System.Text;
6
     using System.Text;
55
             int processId;
54
             int processId;
56
             GetWindowThreadProcessId(handle, out processId);
55
             GetWindowThreadProcessId(handle, out processId);
57
 
56
 
57
+            if (processId == 0)
58
+            {
59
+                return string.Empty;
60
+            }
61
+
58
             var wmiQuery = string.Format("select CommandLine from Win32_Process where ProcessId={0}", processId);
62
             var wmiQuery = string.Format("select CommandLine from Win32_Process where ProcessId={0}", processId);
59
             var searcher = new ManagementObjectSearcher(wmiQuery);
63
             var searcher = new ManagementObjectSearcher(wmiQuery);
60
             var resultEnumerator = searcher.Get().GetEnumerator();
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
         [DllImport("user32.dll")]
69
         [DllImport("user32.dll")]

+ 4
- 0
WindowMonitor/WindowMonitor.csproj View File

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

+ 31
- 2
WindowMonitor/WindowMonitorService.cs View File

1
 namespace WindowMonitor
1
 namespace WindowMonitor
2
 {
2
 {
3
     using System;
3
     using System;
4
+    using System.Linq;
4
     using System.ServiceProcess;
5
     using System.ServiceProcess;
6
+    using System.Text;
7
+    using System.Text.RegularExpressions;
5
     using System.Timers;
8
     using System.Timers;
6
 
9
 
7
     /// <summary>
10
     /// <summary>
19
         /// </summary>
22
         /// </summary>
20
         private WindowMonitor monitor;
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
         /// <summary>
30
         /// <summary>
23
         /// Initializes a new instance of the <see cref="WindowMonitorService"/> class. 
31
         /// Initializes a new instance of the <see cref="WindowMonitorService"/> class. 
24
         /// </summary>
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
             this.InitializeComponent();
38
             this.InitializeComponent();
39
+            this.filterRegex = new Regex(imageFilter);
28
         }
40
         }
29
 
41
 
30
         /// <summary>
42
         /// <summary>
82
         {
94
         {
83
             var handle = this.monitor.GetActiveWindowHandle();
95
             var handle = this.monitor.GetActiveWindowHandle();
84
             Console.WriteLine(this.monitor.GetWindowTitle(handle));
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
             Console.WriteLine();
115
             Console.WriteLine();
87
         }
116
         }
88
     }
117
     }

Loading…
Cancel
Save