c# - 如何仅终止本地计​​算机的远程桌面 session

标签 c# remote-desktop

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

我如何将它保留在运行 session 的本地机器上?

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

无论我运行我的代码时他们如何登录,所有用户都失去了他们的 session 。

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

最佳答案

听起来您试图强制特定用户注销?这是因为您发现用户忘记注销并不断消耗许可证吗?
LabWare 应用程序允许为每个用户设置超时间隔(以分钟为单位),间隔过后,用户将被注销(不再消耗许可证)。
如需更多信息,请参见 LabWare 7 技术手册的第 204 页。
或者,如果这是用于调度程序(服务或集群实例) session ,这也可以由应用程序控制。您可以手动更改服务表上实例记录上的关闭和保持事件标志(如果使用服务管理器),或者您可以编写 LIMS 基本事件触发器/自动化脚本或计划子例程(或将其作为可视化工具上的按钮)工作流)来为你做这件事。
哈。

关于c# - 如何仅终止本地计​​算机的远程桌面 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56166330/

相关文章:

azure - 远程桌面拒绝 Windows Azure Web 角色的凭据

c# - 将 Windows Azure 移动服务添加到 Visual Studio 中的移动应用程序

c# - 在 Linq to DataSet 中为 .Field<T>() 编写通用方法

c# - 无法在 WinForms ComboBox 中更改 DisplayMember

c# - 在未能使用网络级身份验证向远程桌面服务器提供正确凭据后防止登录尝试失败窗口

windows - Windows/Linux 上的多用户登录和远程桌面?

c# - Handler.ashx C# 到 Java、基于 ExtJs 的 TinyMCE 文件和图像管理器插件

c# - ASP.NET C# ListBox 服务器控件不会禁用

c# - 为什么 Clipboard.GetFileDropList() 在远程桌面上返回一个空集合?

windows - 以编程方式识别 Windows RDP session 当前正在进行中