2014-03-02 60 views
0

我對c#很陌生,我想做一個代碼來註銷所有登錄到pc的用戶。 我試着這樣做:如何註銷所有用戶?

System.Diagnostics.Process("shutdown", "/l"); 

但這並沒有註銷所有用戶,該註銷只是我從它運行的用戶。 那麼我該如何做到這一點?

+0

您的程序是否以管理員身份運行? –

+0

看看這裏。可能很容易轉換爲ac#調用:http://social.technet.microsoft.com/Forums/windowsserver/en-US/2da3e724-eddf-4df7-8e0c-3fa545c2fec0/script-to-logoff-all-user -sessions-in-windows-2008-r2?forum = winserverTS –

回答

0

使用WTSDisconnectSession()Windows API。在這裏看到文章。

using System; 
using System.Runtime.InteropServices; 
using System.ComponentModel; 

class Program 
{ 
    [DllImport("wtsapi32.dll", SetLastError = true)] 
    static extern bool WTSDisconnectSession(IntPtr hServer, int sessionId, bool bWait); 

    const int WTS_CURRENT_SESSION = -1; 
    static readonly IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero; 

    static void Main(string[] args) 
    { 
    if (!WTSDisconnectSession(WTS_CURRENT_SERVER_HANDLE, 
     WTS_CURRENT_SESSION, false)) 
     throw new Win32Exception(); 
    } 
} 
+1

看着你的代碼,它只是註銷當前用戶,而不是所有的用戶。 –