.net acquisition system currently logged in user (administrator run equally effective)

Today, learning how to use .Net get under the current system login user name, because there have not seen a more complete article, so finishing the next also share out the last reference documentation that came with convenient get to the bottom of the shoes continue to learn.

First, a brief knowledge

I believe many people first instinct is to use the .net environment variables.

Environment.UserName

But many people may find my article is because of this fact, the environment variables affected an administrator to run, to get that as an administrator, rather than an actual current logged-on user.

The idea here is to use WindowsApi carried out to obtain , through a variety of information to get a search function Api

[DllImport("Wtsapi32.dll")]
protected static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out IntPtr ppBuffer, out uint pBytesReturned);

Second, the specific examples demonstrate how to implement

1. The introduction of API Interface

[DllImport("Wtsapi32.dll")]
protected static extern void WTSFreeMemory(IntPtr pointer);

[DllImport("Wtsapi32.dll")]
protected static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WTSInfoClass wtsInfoClass, out IntPtr ppBuffer, out uint pBytesReturned);

Introduced here WTSFreeMemory method is mainly used for releasing unmanaged resources.

2. WTSInfoClass class definition

public enum WTSInfoClass
{
    WTSInitialProgram,
    WTSApplicationName,
    WTSWorkingDirectory,
    WTSOEMId,
    WTSSessionId,
    WTSUserName,
    WTSWinStationName,
    WTSDomainName,
    WTSConnectState,
    WTSClientBuildNumber,
    WTSClientName,
    WTSClientDirectory,
    WTSClientProductId,
    WTSClientHardwareId,
    WTSClientAddress,
    WTSClientDisplay,
    WTSClientProtocolType,
    WTSIdleTime,
    WTSLogonTime,
    WTSIncomingBytes,
    WTSOutgoingBytes,
    WTSIncomingFrames,
    WTSOutgoingFrames,
    WTSClientInfo,
    WTSSessionInfo
}
View Code

3. Get specific implementation method of the currently logged on user

///  <Summary> 
        /// Gets current user (administrator can be used to run)
         ///  </ Summary> 
        ///  <Returns> </ Returns> 
        public  static  String the GetCurrentUser () 
        { 
            IntPtr Buffer; 
            uint strLen ;
             int cur_session = - . 1 ;
             var username = " the SYSTEM " ; // the ASSUME the this Will return the SYSTEM AS "\ 0" below 
            IF (WTSQuerySessionInformation (IntPtr.Zero, cur_session, WTSInfoClass.WTSUserName, OUT Buffer, OUT strLen) && strLen>1)
            {
                username = Marshal.PtrToStringAnsi(buffer); // don't need length as these are null terminated strings
                WTSFreeMemory(buffer);
                if (WTSQuerySessionInformation(IntPtr.Zero, cur_session, WTSInfoClass.WTSDomainName, out buffer, out strLen) && strLen > 1)
                {
                    username = Marshal.PtrToStringAnsi(buffer) + "\\" + username; // prepend domain name
                    WTSFreeMemory(buffer);
                }
            }
            return username;
        }
View Code

Third, reference documentation

Guess you like

Origin www.cnblogs.com/yokeqi/p/11016800.html