. net检测应用程序启动
本文关键字:启动 应用程序 检测 net | 更新日期: 2023-09-27 18:13:41
我目前正试图监控应用程序的启动和关闭,下面是我当前的代码,但每当它被执行时,我都会收到一个访问拒绝错误,任何深入的想法或原因,为什么会发生这种情况,将非常感谢
class Program
{
static void Main(string[] args)
{
ManagementScope scope = new ManagementScope("root''CIMV2");
scope.Options.EnablePrivileges = true;
try
{
ManagementEventWatcher startWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
startWatch.EventArrived += new EventArrivedEventHandler(startWatch_EventArrived);
startWatch.Start();
ManagementEventWatcher stopWatch = new ManagementEventWatcher(
new WqlEventQuery("SELECT * FROM Win32_ProcessStopTrace"));
stopWatch.EventArrived += new EventArrivedEventHandler(stopWatch_EventArrived);
stopWatch.Start();
Console.WriteLine("Press any key to exit");
while (!Console.KeyAvailable) System.Threading.Thread.Sleep(50);
startWatch.Stop();
stopWatch.Stop();
}
catch (ManagementException e)
{
Console.WriteLine(e);
Console.ReadKey();
}
}
static void stopWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Process stopped: {0}", e.NewEvent.Properties["ProcessName"].Value);
}
static void startWatch_EventArrived(object sender, EventArrivedEventArgs e)
{
Console.WriteLine("Process started: {0}", e.NewEvent.Properties["ProcessName"].Value);
}
}
发生此错误可能是因为您没有在提升模式下运行应用程序。如果在Visual Studio中运行应用程序,请在提升模式下运行Visual Studio,否则在提升模式下运行应用程序exe:
右键单击VS exe/快捷方式,选择"以管理员身份运行"选项。
如果您直接运行您的应用程序exe,然后右键单击您的应用程序exe并选择"以管理员身份运行"