微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何监视应用程序是否被打开?

我正在实施一个桌面分析应用程序,需要logging用户在PC上打开的程序的名称和时间。 这是一个C#(WPF)应用程序,它在用户login并在没有UI的情况下运行时启动。 对于诸如Word或IE之类的程序,它也将捕获他们正在查看的文档或Url。

目前我有一个工作解决scheme如下:

安装一个Windows挂钩的鼠标closures。 当事件触发时,我使用p-Invoke到“GetForegroundWindow”,然后使用窗口句柄“GetwindowThreadProcessId”,使用ProcessId我可以得到包含名称,开始时间和参数启动列表的System.Diagnostics.Process对象。 我维护一个历史列表,所以我只写一个跟踪条目,如果这个processId /窗口句柄组合没有被logging过。

这个解决scheme确实工作正常,但需要鼠标挂钩,可以通过Windows下降没有任何通知或能力来检查是否仍然挂钩。 更何况这个实现看起来像一个黑客。

从C#打开程序 – 也指定工作目录

在IIS 7.5 / Windows 7家庭高级版RavendB错误

如何通过键查找/检查字典值

Windows操作系统体系结构书

如何在Windows中获取login用户的全名?

如果有更好的更直接的方法,请告知。

谢谢。

在扩展打开/保存文件对话框中的视觉风格exception

是否有可用于可移植类库的System.Environment.Machinename的等价物?

Windows如何决定是否触发MouseUp或Click?

授予在没有UAC提示的情况下启动时启动的应用程序的pipe理员权限?

有没有Windows Azure本地开发环境?

您可以使用__InstanceCreationEvent事件和Win32_Process WMI类来监视创建的进程。

试试这个示例C#应用程序

using System; using System.Collections.Generic; using System.Management; using System.Text; namespace GetWMI_Info { public class EventWatcherAsync { private void WmiEventHandler(object sender,EventArrivedEventArgs e) { //in this point the new events arrives //you can access to any property of the Win32_Process class Console.WriteLine("TargetInstance.Handle : " + ((ManagementBaSEObject)e.NewEvent.Properties["TargetInstance"].Value)["Handle"]); Console.WriteLine("TargetInstance.Name : " + ((ManagementBaSEObject)e.NewEvent.Properties["TargetInstance"].Value)["Name"]); } public EventWatcherAsync() { try { string ComputerName = "localhost"; string WmiQuery; ManagementEventWatcher Watcher; ManagementScope Scope; Scope = new ManagementScope(String.Format("\\{0}\root\CIMV2",ComputerName),null); Scope.Connect(); WmiQuery ="Select * From __InstanceCreationEvent Within 1 "+ "Where TargetInstance ISA 'Win32_Process' "; Watcher = new ManagementEventWatcher(Scope,new EventQuery(WmiQuery)); Watcher.EventArrived += new EventArrivedEventHandler(this.WmiEventHandler); Watcher.Start(); Console.Read(); Watcher.Stop(); } catch (Exception e) { Console.WriteLine("Exception {0} Trace {1}",e.Message,e.StackTrace); } } public static void Main(string[] args) { Console.WriteLine("listning process creation,Press Enter to exit"); EventWatcherAsync eventWatcher = new EventWatcherAsync(); Console.Read(); } } }

如果您想要监视Windows上运行的所有内容, PerformanceCounter类就是这种方式。 每次应用程序启动时,窗口都会创建数十个性能计数器,以跟踪应用程序的ProcessID,cpu使用情况,内存使用情况,每秒I / O操作等等。

例如,下面的代码会给你Chrome的进程ID:

PerformanceCounter perf = new PerformanceCounter("Process","ID Process","chrome"); int procId = (int)perf.NextValue();

您还可以使用PerformanceCounterCategory类轻松枚举类别,实例和计数器。

您可以使用Windows的PerfMon工具来了解您将能够检索的信息。 我建议你看一下Process Category(使用PerfMon),在其中你可以找到所有活动进程的列表。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐