如何仅杀死本地计算机的远程桌面会话

时间:2019-05-16 10:23:17

标签: c# remote-desktop

我正在使用以下代码杀死远程桌面会话及其中运行的应用程序。它工作正常,唯一的问题是它会杀死所有用户的指定应用程序。

我如何仅在运行会话的本地计算机上使用它?

我们有多个用户从其本地计算机上的服务器登录并运行此应用程序。大多数使用工作资源运行,但有些使用远程桌面。

无论我在运行代码时如何登录,所有用户都会松开会话。

private void btnCloseSession_Click(object sender, EventArgs e)
    {
        if (!runningExclusiveProcess)
        {
            runningExclusiveProcess = true;
            btnCloseSession.Enabled = false;
                //check and close Labware if running
                if (chkCloseLabware.Checked == true) 
            {
                if (chkExit.Checked == true)
                {
                    KillLabWare();
                    Close();
                }
                else
                {
                    KillLabWare();
                }
            }

            Process[] my = Process.GetProcessesByName("mstsc");

            //loop thru list to get selected item(s)
            ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(lstOpenSessions);
            selectedItems = lstOpenSessions.SelectedItems;

            try
            {
                //remove credentials
                string szTestx = "/delete:GOJO.NET/" + cboServer.Text;
                ProcessStartInfo infox = new ProcessStartInfo("cmdkey.exe", szTestx);
                Process procx = new Process();
                procx.StartInfo = infox;
                procx.Start();

                if (lstOpenSessions.SelectedIndex != -1)
                {
                    for (int i = selectedItems.Count - 1; i >= 0; i--)
                    {
                        //loop thru process to match process vs. list selection(s)
                        foreach (Process remote in my)
                        {
                            if (remote.MainWindowTitle == selectedItems[i].ToString())
                            {
                                KillRS(remote.MainWindowTitle);
                                lstOpenSessions.Items.Remove(selectedItems[i]);
                            }
                        }

                        if (lstOpenSessions.Items.Contains(selectedItems[i].ToString()))
                        {
                            lstOpenSessions.Items.Remove(selectedItems[i]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception caught.", ex);
            }
            // If your task is synchronous, then undo your flag here:
            runningExclusiveProcess = false;
            btnCloseSession.Enabled = true;
        }
    }
    public void KillLabWare()
    {
        ConnectionOptions con = new ConnectionOptions();
        con.Username = cboUserName.Text;
        con.Password = txtPassWord.Text;
        string strIPAddress = cboServer.Text;

        ManagementScope scope = new
            ManagementScope(@"\\" + strIPAddress + @"\root\cimv2", con);
        scope.Connect();
        ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process WHERE Name='Labware.exe'");
        ManagementObjectSearcher searcher = new
            ManagementObjectSearcher(scope, query);
        ManagementObjectCollection objectCollection = searcher.Get();
        foreach (ManagementObject managementObject in objectCollection)
        {
            managementObject.InvokeMethod("Terminate", null);
        }
    }
    private void KillRS(string rwt)
    {
        foreach (Process p in Process.GetProcesses())
        {
            if (p.MainWindowTitle == rwt)
            {
                p.Kill();
            }
        }
    }
    public static void KillRemoteProcess(Process p, string user, string password)
    {
        new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "TaskKill.exe",
                Arguments = string.Format("/pid {0} /s {1} /u {2} /p {3}", p.Id, p.MachineName, user, password),
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true
            }
        }.Start();
    }

2 个答案:

答案 0 :(得分:1)

听起来您正在尝试强制特定用户注销?这是因为您发现用户忘记了注销并不断消耗许可证吗?

LabWare应用程序允许为每个用户设置一个超时间隔(以分钟为单位),在该间隔过去之后,该用户将被注销(不再消耗许可证)。 有关更多信息,请参见《 LabWare 7技术手册》的第204页。

Alternativley,如果这是用于调度程序(服务或群集实例)会话的,则也可以由应用程序控制。您可以手动更改“关闭”并在Services表上的实例记录上保持活动标记(如果使用Service Manager),或者可以编写LIMS Basic事件触发器/自动化脚本或计划的子例程(或在Visual Studio中将其作为按钮)工作流程)为您做到这一点。

HTH。

答案 1 :(得分:0)

如果您想终止远程桌面会话或断开当前的RDP会话,请阅读本文: WTSDisconnectSession function

但是如果您注销当前用户,它也会断开RDP会话,这是代码:

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();
  }
}

我希望它能正常运行,请免费填写以获取更多信息,请评论plz
快乐编码?