如何检测当前登录用户的进程是否正在运行? [英] How to detect if a process is running for the current logged-in user?

查看:127
本文介绍了如何检测当前登录用户的进程是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境-C#、. net 4.0,VS 2010

Environment - C#, .net 4.0, VS 2010

您好,我为Windows编写了一个简单的shell替代品。当用户登录时,shell会自动启动。当用户退出我的shell时,会启动普通的Windows explorer.exe。

Hello, I have written a simple shell replacement for Windows. The shell is launched automatically when users log in. The normal Windows "explorer.exe" is launched when users exit my shell.

现在,当用户退出时(并正确退出)支持此功能)我需要能够检查当前登录用户是否正在运行 explorer.exe。这样可以防止不必要的代码再次启动,从而导致出现 Windows资源管理器应用程序窗口。

Now, when users exit (and to properly support this) I need to be able to check if "explorer.exe" is running for the current logged-in user. This prevents the code from unnecessarily launching it again, which results in a "Windows Explorer" application window.

我已经看到了无数示例来检查和查看进程是否正在运行...但是没有人可以查看当前登录用户是否正在运行。

I have seen countless examples of how to check and see if a process is running...but none to see if it is running for the current logged-in user.

下面的代码将检查 explorer.exe是否为已经运行,如果没有运行,将启动它。但是在某些情况下,此代码在不需要时会测试正值!

The code below will check to see if "explorer.exe" is already running and will start it if it isn't. But there are situations when this code will test positive when it doesn't need to!

例如,在使用快速用户切换时...另一个用户已登录安装到计算机上,结果, explorer.exe显示在进程列表中。但是,在运行 explorer.exe时,当前登录用户未运行它!因此,当我的外壳退出时,代码会测试为肯定,并且不会启动 explorer.exe。用户留下黑屏,没有外壳!

For example, when using fast-user switching...Another user is logged in to the machine and, as a result, "explorer.exe" shows in the process list. But, while "explorer.exe" is running, it is NOT running for the current logged-in user! So when my shell exits, the code tests positive, and "explorer.exe" is not launched. The user is left with a black screen and no shell!

因此,如何修改下面的代码以测试 explorer.exe是否正在针对当前登录状态运行-in用户?

So, how can I mod the code below to test if "explorer.exe" is running for the current logged-in user?

Process[] Processes = Process.GetProcessesByName("explorer");
if (Processes.Length == 0)
{
   string ExplorerShell = string.Format("{0}\\{1}", Environment.GetEnvironmentVariable("WINDIR"), "explorer.exe");
   System.Diagnostics.Process prcExplorerShell = new System.Diagnostics.Process();
   prcExplorerShell.StartInfo.FileName = ExplorerShell;
   prcExplorerShell.StartInfo.UseShellExecute = true;
   prcExplorerShell.Start();
}


推荐答案

您可以从您的会话ID中获取SessionID进程,然后查询具有相同SessionID的进程并获取Explorer实例,假设您的程序名为 NewShell:

You could get SessionID from your process and then query Processes and get Explorer instance that have the same SessionID, let's say that your program is named "NewShell" :

  Process myProc = Process.GetProcesses().FirstOrDefault(pp => pp.ProcessName.StartsWith("NewShell"));
  Process myExplorer = Process.GetProcesses().FirstOrDefault(pp => pp.ProcessName == "explorer" && pp.SessionId == myProc.SessionId);

  if (myExplorer == null)
    StartExplorer()

顺便说一句。如果您使用 ProcessName.StartsWith( NewShell)而不是 ProcessName == NewShell ,则它将在VS调试器(将VSHost添加到exe)

btw. if you use ProcessName.StartsWith("NewShell") instead of ProcessName == "NewShell" then it will work under VS debugger also (it adds vshost to the exe)

这篇关于如何检测当前登录用户的进程是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆