我正在尝试创建一个可以终止终端服务器上用户会话的应用程序。我写了以下代码:
string host = "terminalServer";
string user = "domain\criso";
string sid = "4";
System.Diagnostics.Process proc = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = @"logoff.exe";
startInfo.Arguments = @"/SERVER:" + host + " " + sid;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
proc.StartInfo = startInfo;
proc.Start();
proc.WaitForExit();
// Catch error
if (proc.ExitCode != 0)
{
StreamReader reader = proc.StandardError;
string errorMessage = reader.ReadToEnd();
MessageBox.Show(@"ERROR " + proc.ExitCode.ToString() + ": " + errorMessage);
}
else
StatusLabel.Text = user + @"'s Session terminated";
上面的代码在执行时返回错误“找不到指定的文件”消息。我已经尝试了路径组合到C:\windows\system32\logoff.exe但仍然得到相同的错误消息。
我还尝试使用以下参数调用 cmd.exe 进程:
@"/C logoff /SERVER:" + host + " " + sid
它返回“'logoff' is unrecognized as internal or external command, opreable program or batch file.” 仍然没有运气。
以前有人解决过这个问题吗?有关额外信息,我使用的是 windows 7,终端服务器是 windows server 2003 & 2008 r2(有多个服务器)。如果我直接从命令提示符运行“注销”命令,它可以很好地终止我的会话。