C#获取进程和对进程的操作

时间:2021-07-14 19:50:35 

最近看到一个考试系统,有个功能是用来监视进程的。一旦发现如Communicator.exe这样的违禁软件就立即杀死进程并上报给服务器。我稍 微研究了一下,这个功能实现起来其实很简单。就是使用ManagementObjectSearcher获取进程列表,然后放在一个Collection 里,之后就可以按照自己的逻辑去做了。


using System;
using System.Management;

namespace ConsoleApplication3
{

    class Program
    {
        static void Main(string[] args)
        {
            // Show Process List
            Console.WriteLine("===========Process List===========");
            ManagementObjectCollection objects = new ManagementObjectSearcher("SELECT * FROM Win32_Process").Get();
            foreach (ManagementObject item in objects)
            {
                Console.WriteLine((item["Name"].ToString()));
            }

            // Create Ban List
            Console.WriteLine("===========Ban List===========");
            string lst = "Communicator.exe,POWERPNT.exe,notepad.exe";
            string[] bannedProc = lst.Split(‘,‘);
            foreach (string s in bannedProc)
            {
                Console.WriteLine(s);
            }

            // Search and Destroy
            Console.WriteLine("===========Search and Destroy===========");
            Console.WriteLine("Searching for banned process...");
            int count = 0;
            foreach (string item in bannedProc)
            {
                if (DetectProcess(item))
                {
                    count++;
                    Console.WriteLine("Process [{0}] Detected!", item);
                    Console.WriteLine("[{0}] was killed {1}.", item, KillProcess(item) ? "Successfully" : "Unsucessfully");
                }
            }
            Console.WriteLine("Done, {0} banned process found", count);
        }

        protected static bool DetectProcess(string pProcessName)
        {
            ManagementObjectCollection objects = new ManagementObjectSearcher("SELECT * FROM Win32_Process").Get();
            foreach (ManagementObject item in objects)
            {
                string str = item["Name"].ToString();
                if (str.Trim().ToUpper() == pProcessName.Trim().ToUpper())
                {
                    return true;
                }
            }
            return false;
        }

        public static bool KillProcess(string pProcessName)
        {
            ManagementObjectCollection objects = new ManagementObjectSearcher("SELECT * FROM Win32_Process").Get();
            foreach (ManagementObject item in objects)
            {
                string str = item["Name"].ToString();
                if (str.Trim().ToUpper() == pProcessName.Trim().ToUpper())
                {
                    string[] args = new string[] { "0" };
                    item.InvokeMethod("Terminate", args);
                    return true;
                }
            }
            return false;
        }
    }
}

效果如下:

C#获取进程和对进程的操作

标签:进程,操作
0
投稿

猜你喜欢

  • java开发RocketMQ生产者高可用示例详解

    2023-04-27 13:27:57
  • java编程之基于SpringBoot框架实现扫码登录

    2023-02-14 02:39:28
  • Android实现按钮拖拽还原功能

    2023-10-22 18:31:13
  • java中的Io(input与output)操作总结(四)

    2021-10-11 03:14:19
  • C# XML序列化方法及常用特性总结分析

    2021-12-27 02:23:33
  • C#中实现Fluent Interface的三种方法

    2023-10-04 18:31:03
  • 基于私钥加密公钥解密的RSA算法C#实现方法

    2022-12-01 07:52:37
  • 浅析java 归并排序算法

    2022-01-20 09:59:50
  • C#客户端程序调用外部程序的3种实现方法

    2023-06-02 11:39:10
  • java操作Redis缓存设置过期时间的方法

    2023-09-12 05:49:09
  • javaweb实战之商城项目开发(三)

    2023-04-15 14:27:34
  • C#程序调用cmd.exe执行命令

    2022-11-14 04:04:50
  • SpringCloud入门实验环境搭建

    2022-11-17 10:21:14
  • Java 实战图书管理系统的实现流程

    2023-12-19 05:11:13
  • Android 仿硅谷新闻下拉刷新/上拉加载更多

    2023-03-25 14:19:11
  • java实现数字转换人民币中文大写工具

    2023-08-16 08:48:41
  • spring-boot中使用spring-boot-devtools的实现代码

    2021-10-09 13:31:32
  • Android PullToRefreshLayout下拉刷新控件的终结者

    2021-06-22 02:15:00
  • mybatis映射和实际类型不一致的问题

    2023-08-13 18:09:06
  • JAVA中的字符串常量池使用操作代码

    2021-11-24 02:00:57
  • asp之家 软件编程 m.aspxhome.com