C#练习Array.Sort函数列出所有的进程与模块

简介:     using System; using System.Collections.Generic; using System.Text; using System.

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Collections;

namespace ICompare测试
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] allprocess = Process.GetProcesses();//所有运行的进程
            SortByProcessName cmpproname=new SortByProcessName();
            SortByModuleName cmpmodulename=new SortByModuleName();
            Array.Sort(allprocess, cmpproname);
            foreach (Process p in allprocess)
            {
                Console.WriteLine("{0}", p.ProcessName);
                try
                {
                    
                    ProcessModuleCollection allmodules = p.Modules;//进程下的所有模块
                    ProcessModule[] modules = new ProcessModule[allmodules.Count];
                    int i = 0;
                    foreach (ProcessModule m in allmodules)
                    {
                        modules.SetValue(m, i++);
                    }
                    Array.Sort(modules, cmpmodulename);
                    foreach (ProcessModule m in modules)
                    {
                        try
                        {
                            Console.WriteLine("  {0}", m.ModuleName);
                        }
                        catch { continue; }
                    }
                }
                catch
                { }
               
            }
        }
    }
    public class SortByProcessName : IComparer//Array.Sort()要用到的参数
    {
        public int Compare(object x, object y)
        {
            Process a = (Process)x;
            Process b = (Process)y;
            return a.ProcessName.CompareTo(b.ProcessName);
        }
    }
    public class SortByModuleName : IComparer
    {

        public int Compare(object x, object y)
        {
            ProcessModule a = (ProcessModule)x;
            ProcessModule b = (ProcessModule)y;
            return a.ModuleName.CompareTo(b.ModuleName);
        }
    }
}

 

结果:

  MSIMG32.dll
  msvcrt.dll
  MSVFW32.dll
  MSWSOCK.dll
  ncrypt.dll
  NETAPI32.dll
  netutils.dll
  npmproxy.dll
  NSI.dll
  ntdll.dll
  ntmarta.dll
  ole32.dll
  OLEAUT32.dll
  POWRPROF.dll
  profapi.dll
  propsys.dll
  PSAPI.DLL
  RICHED20.DLL
  RPCRT4.dll
  RpcRtRemote.dll
  rsaenh.dll
  safemon.dll
  SAMCLI.DLL
  sechost.dll
  Secur32.dll
  SensApi.dll
  SETUPAPI.dll
  SHELL32.dll
  SHLWAPI.dll
  slc.dll
  srvcli.dll
  SspiCli.dll
  TeamViewer.exe
  TeamViewer_Resource_zhCN.dll
  TeamViewer_StaticRes.dll
  tv_w32.dll
  urlmon.dll
  USER32.dll
  USERENV.dll
  USP10.dll
  uxtheme.dll
  VERSION.dll
  wdmaud.drv
  webio.dll
  winhttp.dll
  WININET.dll
  WINMM.dll
  WINNSI.DLL
  winsta.dll
  WINTRUST.dll
  wkscli.dll
  WLDAP32.dll
  WS2_32.dll
  wshtcpip.dll
  WTSAPI32.dll
TeamViewer_Service
  ADVAPI32.dll
  apphelp.dll
  bcrypt.dll
  bcryptprimitives.dll
  browcli.dll
  CFGMGR32.dll
  CLBCatQ.DLL
  comctl32.dll
  CRYPT32.dll
  CRYPTBASE.dll
  CRYPTSP.dll
  cscapi.dll
  d3d8thk.dll
  d3d9.dll
  davclnt.dll
  DAVHLPR.dll
  DEVOBJ.dll
  dhcpcsvc.DLL
  DNSAPI.dll
  drprov.dll
  dwmapi.dll
  fastprox.dll
  fwpuclnt.dll
  GDI32.dll
  gdiplus.dll
  GPAPI.dll
  iertutil.dll
  imagehlp.dll
  IMM32.DLL
  IPHLPAPI.DLL
  kernel32.dll
  KERNELBASE.dll
  LPK.dll
  Magnification.dll
  MPR.dll
  MSASN1.dll
  MSCTF.dll
  msvcrt.dll
  MSWSOCK.dll
  napinsp.dll
  ncrypt.dll
  Netapi32.dll
  netprofm.dll
  netutils.dll
  NLAapi.dll
  Normaliz.dll
  npmproxy.dll
  NSI.dll
  ntdll.dll
  NTDSAPI.dll
  ntlanman.dll
  ntmarta.dll
  ole32.dll
  OLEAUT32.dll
  pnrpnsp.dll
  profapi.dll
  propsys.dll
  Psapi.dll
  rasadhlp.dll
  RASAPI32.dll
  rasman.dll
  RPCRT4.dll
  RpcRtRemote.dll
  rsaenh.dll
  rtutils.dll
  sechost.dll
  Secur32.dll
  SensApi.dll
  SETUPAPI.dll
  SHELL32.dll
  SHLWAPI.dll
  srvcli.dll
  SspiCli.dll
  SXS.DLL
  TeamViewer_Service.exe
  urlmon.dll
  USER32.dll
  USERENV.dll
  USP10.dll
  uxtheme.dll
  VERSION.dll
  wbemcomn.dll
  wbemprox.dll
  wbemsvc.dll
  WININET.dll
  WINNSI.DLL
  winrnr.dll
  winsta.dll
  WINTRUST.dll
  wkscli.dll
  WLDAP32.dll
  WS2_32.dll
  wshbth.dll
  wship6.dll
  wshtcpip.dll
  WTSAPI32.dll
TSVNCache
tv_w32
  ADVAPI32.dll
  CFGMGR32.dll
  COMCTL32.dll
  CRTDLL.dll
  CRYPTBASE.dll
  DEVOBJ.dll
  devrtl.DLL
  GDI32.dll
  IMM32.DLL
  kernel32.dll
  KERNELBASE.dll
  LPK.dll
  MSCTF.dll
  msvcrt.dll
  newdev.dll
  ntdll.dll
  ole32.dll
  OLEAUT32.dll
  RPCRT4.dll
  sechost.dll
  setupapi.dll
  SHELL32.dll
  SHLWAPI.dll
  SspiCli.dll
  tv_w32.dll
  tv_w32.exe
  USER32.dll
  USP10.dll
  UxTheme.dll
tv_x64
TXPlatform
  ADVAPI32.dll
  CLBCatQ.DLL
  COMCTL32.dll
  CRTDLL.dll
  CRYPT32.dll
  CRYPTBASE.dll
  CRYPTSP.dll
  GDI32.dll
  iertutil.dll
  IMM32.DLL
  kernel32.dll
  KERNELBASE.dll
  LPK.dll
  MSASN1.dll
  MSCTF.dll
  msvcrt.dll
  NSI.dll
  ntdll.dll
  ole32.dll
  OLEAUT32.dll
  PSAPI.DLL
  RPCRT4.dll
  RpcRtRemote.dll
  rsaenh.dll
  safemon.dll
  sechost.dll
  SHELL32.dll
  SHLWAPI.dll
  SspiCli.dll
  SXS.DLL
  tv_w32.dll
  TXPFProxy.dll
  TXPlatform.exe
  urlmon.dll
  USER32.dll
  USP10.dll
  uxtheme.dll
  VERSION.dll
  WININET.dll
  WINTRUST.dll
  WS2_32.dll
wininit
winlogon
ZhuDongFangYu
  360base.dll
  360conf.dll
  360leakfixplugin.dll
  360NetBase.dll
  360SoftMgrS.dll
  ADVAPI32.dll
  apphelp.dll
  CFGMGR32.dll
  CLBCatQ.DLL
  cloudcom2.dll
  COMCTL32.dll
  CRYPT32.dll
  CRYPTBASE.dll
  DEVOBJ.dll
  dhcpcsvc.DLL
  dhcpcsvc6.DLL
  DNSAPI.dll
  fltlib.dll
  GDI32.dll
  heavygate.dll
  iertutil.dll
  IMM32.DLL
  IPHLPAPI.DLL
  kernel32.dll
  KERNELBASE.dll
  LINKINFO.dll
  LPK.dll
  MSASN1.dll
  MSCTF.dll
  mstask.dll
  msvcrt.dll
  mswsock.dll
  NETAPI32.dll
  netutils.dll
  NSI.dll
  ntdll.dll
  ole32.dll
  OLEAUT32.dll
  profapi.dll
  propsys.dll
  PSAPI.DLL
  qutmload.dll
  RASAPI32.dll
  rasman.dll
  RPCRT4.dll
  RpcRtRemote.dll
  rtutils.dll
  SAMCLI.DLL
  SAMLIB.dll
  sechost.dll
  secur32.dll
  SETUPAPI.dll
  SHELL32.dll
  SHLWAPI.dll
  srvcli.dll
  SspiCli.dll
  taskschd.dll
  urlmon.dll
  USER32.dll
  USERENV.dll
  USP10.dll
  VERSION.dll
  WININET.dll
  WINMM.dll
  WINNSI.DLL
  WINTRUST.dll
  wkscli.dll
  WS2_32.dll
  WTSAPI32.dll
  zhudongfangyu.exe
请按任意键继续. . .

相关文章
|
26天前
|
C# Python
使用wxpython开发跨平台桌面应用,对wxpython控件实现类似C#扩展函数处理的探究
【10月更文挑战第30天】使用 `wxPython` 开发跨平台桌面应用时,可以通过创建辅助类来模拟 C# 扩展函数的功能。具体步骤包括:1. 创建辅助类 `WxWidgetHelpers`;2. 在该类中定义静态方法,如 `set_button_color`;3. 在应用中调用这些方法。这种方法提高了代码的可读性和可维护性,无需修改 `wxPython` 库即可为控件添加自定义功能。但需要注意显式调用方法和避免命名冲突。
|
1月前
|
安全 API C#
C# 如何让程序后台进程不被Windows任务管理器强制结束
C# 如何让程序后台进程不被Windows任务管理器强制结束
65 0
|
2月前
|
Linux C语言
C语言 多进程编程(三)信号处理方式和自定义处理函数
本文详细介绍了Linux系统中进程间通信的关键机制——信号。首先解释了信号作为一种异步通知机制的特点及其主要来源,接着列举了常见的信号类型及其定义。文章进一步探讨了信号的处理流程和Linux中处理信号的方式,包括忽略信号、捕捉信号以及执行默认操作。此外,通过具体示例演示了如何创建子进程并通过信号进行控制。最后,讲解了如何通过`signal`函数自定义信号处理函数,并提供了完整的示例代码,展示了父子进程之间通过信号进行通信的过程。
|
2月前
|
编译器
【收藏】内核级利用通用Hook函数方法检测进程
【收藏】内核级利用通用Hook函数方法检测进程
|
2月前
|
SQL 网络协议 数据库连接
已解决:连接SqlServer出现 provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程【C#连接SqlServer踩坑记录】
本文介绍了解决连接SqlServer时出现“provider: Shared Memory Provider, error: 0 - 管道的另一端上无任何进程”错误的步骤,包括更改服务器验证模式、修改sa用户设置、启用TCP/IP协议,以及检查数据库连接语句中的实例名是否正确。此外,还解释了实例名mssqlserver和sqlserver之间的区别,包括它们在默认设置、功能和用途上的差异。
|
3月前
|
数据采集 并行计算 安全
Python并发编程:多进程(multiprocessing模块)
在处理CPU密集型任务时,Python的全局解释器锁(GIL)可能会成为瓶颈。为了充分利用多核CPU的性能,可以使用Python的multiprocessing模块来实现多进程编程。与多线程不同,多进程可以绕过GIL,使得每个进程在自己的独立内存空间中运行,从而实现真正的并行计算。
|
3月前
|
JavaScript 前端开发 PHP
|
3月前
|
Linux API
Linux源码阅读笔记07-进程管理4大常用API函数
Linux源码阅读笔记07-进程管理4大常用API函数
|
3月前
|
存储 安全 Python
[python]使用标准库logging实现多进程安全的日志模块
[python]使用标准库logging实现多进程安全的日志模块
|
3月前
|
存储 关系型数据库 MySQL