Aller au contenu
Règlement du forum ×
IPTV et arnaques ×

recuperer le nom d'utilisateur c#


zaki

Messages recommandés

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

Lien vers le commentaire
Partager sur d’autres 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

Modifié par Darkvader
Lien vers le commentaire
Partager sur d’autres 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 !:(

Lien vers le commentaire
Partager sur d’autres sites

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.

Invité
Répondre à ce sujet…

×   Collé en tant que texte enrichi.   Coller en tant que texte brut à la place

  Seulement 75 émoticônes maximum sont autorisées.

×   Votre lien a été automatiquement intégré.   Afficher plutôt comme un lien

×   Votre contenu précédent a été rétabli.   Vider l’éditeur

×   Vous ne pouvez pas directement coller des images. Envoyez-les depuis votre ordinateur ou insérez-les depuis une URL.

×
×
  • Créer...