欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

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

时间:2021-01-11 10:59:56|栏目:.NET代码|点击:

最近看到一个考试系统,有个功能是用来监视进程的。一旦发现如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# 学习笔记续

栏    目:.NET代码

下一篇:在ASP.NET 2.0中操作数据之六十五:在TableAdapters中创建新的存储过程

本文标题:C#获取进程和对进程的操作

本文地址:http://www.codeinn.net/misctech/43462.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有