Jump to content
Règlement du forum ×
IPTV et arnaques ×

recuperer le nom d'utilisateur c#


zaki

Recommended Posts

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 !! :mad:

 

si qlq 'un peut m'aider

merci!!

Link to comment
Share on other sites

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 !! :mad:

 

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 by Darkvader
Link to comment
Share on other sites

:)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 !:(

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...