C#.NET如何检测,如果一个进程正在运行,为当前登录的用户?正在运行、进程、用户、NET

2023-09-06 17:42:00 作者:偷腥的小野猫

环境 - C#,.NET 4.0,VS 2010

您好,我写了一个简单的外壳更换为Windows。外壳将自动​​启动,当用户的正常的Windows的explorer.exe启动登录,当用户退出我的壳。

现在,当用户退出(和正确支持这个)我需要能够检查是否Explorer.exe的正在运行当前登录的用户。这$ P $不必要地再次启动它,这会导致Windows资源管理器应用程序窗口pvents的code。

我已经看到了如何检查,看看进程正在运行...但没有,看它是否正在运行当前登录用户的无数例子。

在code将在下面检查,看看是否的explorer.exe已经运行,并启动它,如果它不是。但是有些时候,这个code将检测为阳性时,它并不需要!情况

网卡监测工具 AMP NetMonitor 监测网络适配器的网络活动 V1.0.1

例如,使用快速用户切换时...另一个用户登录到计算机上,并作为一个结果,的explorer.exe进程列表中显示。但是,尽管Explorer.exe的正在运行,它不是为当前登录的用户运行!所以,当我的壳退出时,code试验阳性,Explorer.exe的没有启动。该用户留下了一个黑色的屏幕和无壳!

所以,我怎么能mod低于code,以测试的explorer.exe运行为当前登录的用户?

 过程[]制程= Process.GetProcessesByName(资源管理器);
如果(Processes.Length == 0)
{
   串ExplorerShell =的String.Format({0} \\ {1},Environment.GetEnvironmentVariable(WINDIR),explorer.exe的);
   的System.Diagnostics.Process prcExplorerShell =新的System.Diagnostics.Process();
   prcExplorerShell.StartInfo.FileName = ExplorerShell;
   prcExplorerShell.StartInfo.UseShellExecute =真;
   prcExplorerShell.Start();
}
 

解决方案

您可以得到的SessionID从你的过程,然后查询进程,让浏览器实例具有相同的SessionID,让我们说,你的程序被命名为NewShell:

 过程MYPROC = Process.GetProcesses()FirstOrDefault(PP => pp.ProcessName.StartsWith(NewShell))。
  流程myExplorer = Process.GetProcesses()FirstOrDefault(PP => pp.ProcessName ==资源管理器和安培;&安培; pp.SessionId == myProc.SessionId)。

  如果(myExplorer == NULL)
    StartExplorer()
 

BTW。如果你使用 ProcessName.StartsWith(NewShell)而不是 ProcessName ==NewShell然后它会工作在VS调试器还(它增加了vshost的exe文件)

Environment - C#, .net 4.0, VS 2010

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.

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.

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!

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!

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();
}

解决方案

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()

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)

 
精彩推荐
图片推荐