首页 > 代码库 > c#中进程的使用
c#中进程的使用
C#的Process类的一些用法
更多0
c#
进程
c#之process类相关整理
一、根据进程名获取进程的用户名?
需要添加对 System.Management.dll 的引用
using System.Diagnostics;using System.Management;static void Main(string[] args){foreach (Process p in Process.GetProcesses()) { Console.Write(p.ProcessName); Console.Write("----"); Console.WriteLine(GetProcessUserName(p.Id)); } Console.ReadKey();}private static string GetProcessUserName(int pID){ string text1 = null; SelectQuery query1 = new SelectQuery("Select * from Win32_Process WHERE processID=" + pID); ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1); try { foreach (ManagementObject disk in searcher1.Get()) { ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; inPar = disk.GetMethodParameters("GetOwner"); outPar = disk.InvokeMethod("GetOwner", inPar, null); text1 = outPar["User"].ToString(); break; } } catch { text1 = "SYSTEM"; } return text1;}
进程名 是 p.ProcessName
二、获取进程
using System.Diagnostics;//【注】System.Diagnostics 命名空间提供特定的类,使您能够与系统进程、事件日志和性能计数器进行交互。string str = "";Process[] processes;//Get the list of current active processes.processes = System.Diagnostics.Process.GetProcesses();//Grab some basic information for each process.Process process;for(int i = 0;i<processes.Length-1;i++){process = processes[i];str = str + Convert.ToString(process.Id) + " : " +process.ProcessName + "\r\n";}System.Windows.Forms.MessageBox.Show(str);txtProcessID.Text = processes[0].Id.ToString();//显示进程相关信息string s = "";System.Int32 processid;Process process;processid = Int32.Parse(txtProcessID.Text);process = System.Diagnostics.Process.GetProcessById(processid);s = s + "该进程的总体优先级类别:" + Convert.ToString(process.PriorityClass) + " \r\n";s = s + "由该进程打开的句柄数:" + process.HandleCount + "\r\n";s = s + "该进程的主窗口标题:" + process.MainWindowTitle + "\r\n";s = s + " 该进程允许的最小工作集大小:" + process.MinWorkingSet.ToString() + " \r\n";s = s + "该进程允许的最大工作集大小:"