2013-09-05 36 views
1

如何從Windows 7 64位系統中的C#Windows服務獲取當前登錄的用戶名。在64位Windows 7的C#窗口服務中獲取Windows用戶名?

我用下面的代碼在32位Windows正常工作:

ManagementObjectSearcher searcher = 
     new ManagementObjectSearcher("SELECT UserName from Win32_ComputerSystem"); 

ManagementObjectCollection collection = searcher.Get(); 

string username = 
     (string)collection.Cast<ManagementBaseObject>().First()["UserName"]; 

但不能在64位上運行。請建議

謝謝

+2

「但不能在64位上運行」......並且您希望我們以某種方式神奇地幫助您解決您未指定的問題?發佈您的問題,我們可能會提供幫助。你有錯誤信息嗎?你什麼時候得到它,在編譯時還是在運行時? – nvoigt

+0

當多個用戶登錄時,您會得到什麼結果? – Alex

+0

1.找不到指定的模塊。在System.Management.ManagementScope.InitializeGuts(對象O) 2. 3. System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(的Int32的errorCode,IntPtr的errorInfo中) 在System.Management.ManagementScope:(0x8007007E從HRESULT異常)在System.Management.ManagementObjectSearcher.Initialize()012處的初始化() System.Management.ManagementObjectSearcher.Get() –

回答

-1

使用此命令獲取當前登錄的用戶。如果它有效,請將其標記爲答案。

System.Security.Principal.WindowsIdentity.GetCurrent().Name; 
1

一個簡單的解決方案是使用通常包含登錄用戶名的Environment.UserName。

有些情況下服務正在運行,沒有用戶登錄和Environment.UserName爲空或在env。變量被刪除了一些奇怪的原因。你可以使這個變量在這種情況下是必需的。

System.Security.Principal.WindowsIdentity.GetCurrent()在Environment.UserName的更多方案中可能爲null,因此請注意在調用System.Security之前不要調用System.Security.Principal.WindowsIdentity.GetCurrent()。 .Principal.WindowsIdentity.GetCurrent()確實存在。

如果你想弄清楚當前登錄的終端服務會話,你可以使用下面的類中:

public class TerminalServices { 
    [DllImport("wtsapi32.dll", SetLastError = true)] 
    static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName); 

    [DllImport("wtsapi32.dll")] 
    static extern void WTSCloseServer(IntPtr hServer); 

    [DllImport("wtsapi32.dll", SetLastError=true)] 
    static extern Int32 WTSEnumerateSessions(IntPtr hServer, [MarshalAs(UnmanagedType.U4)] Int32 Reserved, [MarshalAs(UnmanagedType.U4)] Int32 Version, ref IntPtr ppSessionInfo, [MarshalAs(UnmanagedType.U4)] ref Int32 pCount); 

    [DllImport("wtsapi32.dll")] 
    static extern void WTSFreeMemory(IntPtr pMemory); 

    [StructLayout(LayoutKind.Sequential)] 
    private struct WTS_SESSION_INFO { 
     public Int32 SessionID; 

     [MarshalAs(UnmanagedType.LPStr)] 
     public String pWinStationName; 

     public WTS_CONNECTSTATE_CLASS State; 
    } 

    public enum WTS_CONNECTSTATE_CLASS { 
     WTSActive, 
     WTSConnected, 
     WTSConnectQuery, 
     WTSShadow, 
     WTSDisconnected, 
     WTSIdle, 
     WTSListen, 
     WTSReset, 
     WTSDown, 
     WTSInit 
    } 

    public static IntPtr OpenServer(String Name) { 
     IntPtr server = WTSOpenServer(Name); 
     return server; 
    } 

    public static void CloseServer(IntPtr ServerHandle) { 
     WTSCloseServer(ServerHandle); 
    } 

    public static List<Session> ListSessions(String ServerName) { 
     var ret = new List<Session>(); 
     var server = OpenServer(ServerName); 

     try { 
      IntPtr ppSessionInfo = IntPtr.Zero; 
      Int32 count = 0; 
      Int32 retval = WTSEnumerateSessions(server, 0, 1, ref ppSessionInfo, ref count); 
      Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); 
      Int64 current = (int) ppSessionInfo; 

      if (retval != 0) { 
       for (int i=0; i<count; i++) { 
        WTS_SESSION_INFO si = (WTS_SESSION_INFO) Marshal.PtrToStructure((System.IntPtr) current, typeof(WTS_SESSION_INFO)); 
        current += dataSize; 

        ret.Add(new Session { Id = si.SessionID, State = si.State.ToString(), Type = si.pWinStationName }); 
       } 

       WTSFreeMemory(ppSessionInfo); 
      } 
     } finally { 
      CloseServer(server); 
     } 

     return ret; 
    } 
} 

public class Session { 
    public int Id { 
     get; 
     set; 
    } 

    public string State { 
     get; 
     set; 
    } 

    public string Type { 
     get; 
     set; 
    } 
} 

TerminalServices.ListSessions會告訴你有多少用戶,什麼用戶登錄進入該終端。