简体   繁体   English

无法获取正在运行/当前进程的列表?

[英]Cannot get the list of running/current processes?

I'm trying to get the list of running processes / sessions on my computer with Win7 using the following code, which is found in internet. 我正在尝试使用Internet上的以下代码来获取Win7计算机上正在运行的进程/会话的列表。 However it doesn't work? 但是它不起作用吗? I have not got any compile errors. 我没有任何编译错误。

namespace CurrentProcessesLister
{
   class Program
   {
      static void Main(string[] args)
      {
          Process[] runningProcesses = Process.GetProcesses();
          var currentSessionID = Process.GetCurrentProcess().SessionId; 
          Process[] sameAsthisSession = (from c in runningProcesses where c.SessionId ==    currentSessionID select c).ToArray(); 

          foreach (var p in sameAsthisSession) 
          { 
              Trace.WriteLine(p.ProcessName); 
          }
      }
  }
}

" there is no output in the console " “控制台中没有输出”

Then replace 然后更换

Trace.WriteLine(p.ProcessName); 

with

Console.WriteLine(p.ProcessName); 

But if you then still don't see anything, it's time to start using the debugger. 但是,如果您仍然看不到任何内容,那么该开始使用调试器了。

The problem is you are using Trace.WriteLine to output the text. 问题是您正在使用Trace.WriteLine输出文本。 This is used for program tracing and won't show up by default in the console. 这用于程序跟踪,默认情况下不会显示在控制台中。 Use Console.WriteLine instead. 请改用Console.WriteLine

  foreach (var p in sameAsthisSession) 
  { 
      Console.WriteLine(p.ProcessName); 
  }

Here is a Quick and Dirty way... 这是一种快速而肮脏的方式...

  Process[] proc = Process.GetProcesses();
  foreach(Process theprocess in proc)
  {
    Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
  }

To further expand on what the previous posts have mentioned, the trace output does indeed show up but you need to look in the debugger output window in Visual Studio. 为了进一步扩展前面的文章中提到的内容,确实会显示跟踪输出,但是您需要查看Visual Studio中的调试器输出窗口。 The code you originally posted does indeed list the processes under the Debug section in the output window. 您最初发布的代码确实确实在输出窗口的“调试”部分下列出了进程。

'ConsoleApplication1.Program..' 'ConsoleApplication1.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\\windows\\Microsoft.Net\\assembly\\GAC_MSIL\\System.Configuration\\v4.0_4.0.0.0__b03f5f7f11d50a3a\\System.Configuration.dll', Skipped loading symbols. 'ConsoleApplication1.Program ..''ConsoleApplication1.vshost.exe'(受管理(v4.0.30319)):已加载'C:\\ windows \\ Microsoft.Net \\ assembly \\ GAC_MSIL \\ System.Configuration \\ v4.0_4.0.0.0__b03f5f7f11d50a3a \\ System.Configuration.dll',跳过加载符号。 Module is optimized and the debugger option 'Just My Code' is enabled. 模块已优化,调试器选项“ Just My Code”已启用。
devenv 德文
winlogon etc... Winlogon等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM