在windows底层的api中,提供了以下的方法来满足需求:
WTSGetActiveConsoleSessionId: 获取当前active User的 session ID。
CreateProcessAsUser:用当前用户创建一个新的进程。
- public static string GetCurrentActiveUser()
- {
- IntPtr hServer = IntPtr.Zero, state = IntPtr.Zero;
- uint bCount = 0;
-
- // obtain the currently active session id; every logged on user in the system has a unique session id
- uint dwSessionId = WTSGetActiveConsoleSessionId();
- string domain = string.Empty, userName = string.Empty;
-
- if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.DomainName, out state, out bCount))
- {
- domain = Marshal.PtrToStringAuto(state);
- }
-
- if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.UserName, out state, out bCount))
- {
- userName = Marshal.PtrToStringAuto(state);
- }
-
- return string.Format("{0}\\{1}", domain, userName);
- }
相关热门文章
给主人留下些什么吧!~~
评论热议