zaki Posté(e) le 15 octobre 2008 Share Posté(e) le 15 octobre 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!! Citer Lien vers le commentaire Partager sur d’autres sites More sharing options...
Darkvader Posté(e) le 15 octobre 2008 Share Posté(e) le 15 octobre 2008 (modifié) 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 Modifié le 15 octobre 2008 par Darkvader Citer Lien vers le commentaire Partager sur d’autres sites More sharing options...
zaki Posté(e) le 15 octobre 2008 Auteur Share Posté(e) le 15 octobre 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 ! Citer Lien vers le commentaire Partager sur d’autres sites More sharing options...
Messages recommandés
Rejoindre la conversation
Vous pouvez publier maintenant et vous inscrire plus tard. Si vous avez un compte, connectez-vous maintenant pour publier avec votre compte.