2014-03-02 61 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

看着你的代码,它只是注销当前用户,而不是所有的用户。 –