C# 获取系统进程的用户名

需要添加对 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;
}

(0)

相关推荐

  • 使用C#获取系统特殊文件夹路径的解决方法

    win7下无法向c盘写入文件,当前用户只能向自己的用户文件夹写入文件,比如MyDocuments,文件夹,用c#得到这些文件夹的目录方法是: 复制代码 代码如下: string path=System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  Environment.SpecialFolder 枚举说明: CommonApplicationData 目录,它用作所有用户使用的应用程序特定数据的公共储存库.

  • C# 当前系统时间获取及时间格式详解

    C# 当前系统时间获取及时间格式 最近学习C# 的知识,对获取系统时间和时间格式进行了总结,这是本文在网上整理的详细资料,大家看下! --DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 取当前年月日时分秒 currentTime=System.DateTime.Now; 取当前年 int 年=currentTime.Year; 取当前月 int 月=currentTime.Month; 取当前日 int 日=curr

  • c# socket编程udp客户端实现代码分享

    复制代码 代码如下: Console.WriteLine("This is a Client, host name is {0}", Dns.GetHostName());//设置服务端终结点IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);//创建与服务端连接的套接字,指定网络类型,数据连接类型和网络协议Socket ConnSocket = new Socket(Address

  • C#如何取硬件标志

    using System; using System.Runtime.InteropServices; using  System.Management; namespace Hardware { /// <summary> /// Hardware_Mac 的摘要说明. /// </summary> public class HardwareInfo {   //取机器名    public string GetHostName()   {    return System.Ne

  • 使用C#配合ArcGIS Engine进行地理信息系统开发

    简单的地图读取.展示 终于到暑假了...开始认真整理整理相关学习的心得体会咯~ 先把很久之前挖的关于C# 二次开发的坑给填上好了~ 这次先计划用一个月把C# ArcEngine 10.0相关开发的学习心得给发布出来好啦~ 第一部分就是最简单的helloworld了:掌握使用控件创建简单的GIS应用程序~ (前期相关环境配置略掉~请自行百度~) 首先打开VS2010,,通过(文件--新建--项目--Windos窗体应用程序) ,我们新建一个名叫"MyHelloWorld"的Windows

  • C#获取系统版本信息方法

    直接贴代码: 复制代码 代码如下: public class OSInfoMation { public static string OSBit() { try { ConnectionOptions oConn = new ConnectionOptions(); System.Management.ManagementScope managementScope = new System.Management.ManagementScope("\\\\localhost", oCon

  • 分享用于操作FTP的客户端C#类

    这是一个用于操作FTP的客户端C#类,类已经封装好了各种常用的Ftp操作方法,调用非常简单,你不需要关心ftp连接和操作的细节,只要调用这个类里的相关方法就可以了. using System; using System.Net; using System.IO; using System.Text; using System.Net.Sockets; using System.Threading; namespace DotNet.Utilities { public class FTPClie

  • 在C#中对TCP客户端的状态封装详解

    TCP客户端连接TCP服务器端有几种应用状态:1.与服务器的连接已建立2.与服务器的连接已断开3.与服务器的连接发生异常 应用程序可按需求合理处理这些逻辑,比如:1.连接断开后自动重连2.连接断开后选择备用地址重连3.所有状态变化上报告警本文描述的TcpClient实现了状态变化的事件通知机制. 复制代码 代码如下: /// <summary>   /// 异步TCP客户端   /// </summary>   public class AsyncTcpClient : IDisp

  • 获取客户端IP地址c#/vb.net各自实现代码

    公司的域环境内,程序要求获取客户端的IP地址,分部程序码分享于此. C#: VB.NET:

  • C#编程获取客户端计算机硬件及系统信息功能示例

    本文实例讲述了C#编程获取客户端计算机硬件及系统信息功能.分享给大家供大家参考,具体如下: 这里使用C#获取客户端计算机硬件及系统信息 ,包括CPU.硬盘.IP.MAC地址.操作系统等. 1.项目引用System.Management库. 2.创建HardwareHandler.cs类文件 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Manag

  • C#实现的Socket服务器端、客户端代码分享

    服务端: using System; using System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Text; namespace Server { class Program { static void Main(string[] args) { Socket server = new Socket(AddressFamily.InterNetwork, SocketType

  • C#用Activex实现Web客户端读取RFID功能的代码

    由于要在Web项目中采用RFID读取功能,所以有必要开发Activex,一般情况下开发Activex都采用VC,VB等,但对这两块不是很熟悉,所以采用C#编写Activex的方式实现. 本文方法参考网络 1.编写WindowsFromControls 2.发布WindowsFormControls为Activex 3.在web中使用该Activex 首先编写windows控件 如何编写不再详述(注意一个地方,GUID自己用vs工具生成一个,下面会用到.我的0CBD6597-3953-4B88-8

  • C#实现获取运行平台系统信息的方法

    本文实例讲述了C#获取运行平台系统信息的方法,主要可以实现C#获取系统启动经过的毫秒数,相连网络域名,系统启动经过的毫秒数等,并有关于ListView控件的相关操作. 具体的实现代码如下: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace 获取系统环境和

随机推荐