zaki Posted October 15, 2008 Share Posted October 15, 2008 Bonjour les amis comment récupérer le Nom d'utilisateur d'un processus en c# par exemple j'ai essayé ca foreach (Process proc in Process.GetProcesses()) { Console.WriteLine(proc.ProcessName+" "+proc.StartInfo.UserName); } mais ca n'affiche rien !! si qlq 'un peut m'aider merci!! Quote Link to comment Share on other sites More sharing options...
Darkvader Posted October 15, 2008 Share Posted October 15, 2008 (edited) Bonjour les amis comment récupérer le Nom d'utilisateur d'un processus en c# par exemple j'ai essayé ca foreach (Process proc in Process.GetProcesses()) { Console.WriteLine(proc.ProcessName+" "+proc.StartInfo.UserName); } mais ca n'affiche rien !! si qlq 'un peut m'aider merci!! UserName doesn't work here since it's managed code, and the only way to get that info is to start a process with username. as for a solution here is one: using System; using System.Text; using System.Diagnostics; using System.Management; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { foreach (Process proc in Process.GetProcesses()) { Console.WriteLine("ProcessID: " + proc.Id.ToString() + ", Name: " + proc.ProcessName + ", UserName:" + GetProcessOwner(proc.Id)); } Console.ReadLine(); } static string GetProcessOwner(int processId) { string query = "Select * FROM Win32_Process WHERE ProcessID = " + processId; ManagementObjectSearcher mos = new ManagementObjectSearcher(query); ManagementObjectCollection procList = mos.Get(); foreach (ManagementObject obj in procList) { string[] procargList = new string[] { string.Empty}; int retVal = int.Parse(obj.InvokeMethod("GetOwner", procargList).ToString()); if (retVal == 0) return procargList[0]; } return "None"; } } } Writing in-process shell extensions in managed code is actually a very dangerous thing to do because it has the effect of injecting your managed code (and the .NET Framework) into every application on the machine that has a file open dialog, so use this code at your own risk. It is also extremely slow Edited October 15, 2008 by Darkvader Quote Link to comment Share on other sites More sharing options...
zaki Posted October 15, 2008 Author Share Posted October 15, 2008 :)merci bcp darkvader mais en lisant ca : Writing in-process shell extensions in managed code is actually a very dangerous thing to do because it has the effect of injecting your managed code (and the .NET Framework) into every application on the machine that has a file open dialog, so use this code at your own risk. It is also extremely slow je ne crois pas que je vais integrer ce Code a mon Programme ! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.