c# get username

135

c# get username -

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

c# get username -

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;

c# get logged on user name -

foreach (System.Management.ManagementObject Process in Processes.Get())
{
    if (Process["ExecutablePath"] != null && 
        System.IO.Path.GetFileName(Process["ExecutablePath"].ToString()).ToLower() == "explorer.exe" )
    {
        string[] OwnerInfo = new string[2];
        Process.InvokeMethod("GetOwner", (object[])OwnerInfo);

        Console.WriteLine(string.Format("Windows Logged-in Interactive UserName={0}", OwnerInfo[0]));

        break;
    }
}

Comments

Submit
0 Comments