如何仅断开与LOCAL远程应用程序的连接,其次如何仅在LOCAL远程桌面上退出应用程序? [英] How do I disconnect from LOCAL remote app only, secondly how do I exit a application on LOCAL remote desktop only?

查看:114
本文介绍了如何仅断开与LOCAL远程应用程序的连接,其次如何仅在LOCAL远程桌面上退出应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



如何将它保留在运行会话的本地计算机上?



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



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



I'm using the following code to kill a remote desktop session and the application running in it. It works fine, the only problem is that it kills the specified server of the application for all users.

How do I keep this to just the local machine running a session?

We have multiple users logging in and running this application from a server on their local machines. Most are running using work resources, but some use remote desktop.

No matter how they are logged in when I run my code all users loose their sessions.

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





我的尝试:



我已经尝试了上面的代码,并尝试更改查询的范围。



也发布在Stack Overflow上,没有响应。



What I have tried:

I have tried the above code and tried changing the scope of the query.

Also posted this on Stack Overflow, with no response.

推荐答案

你正在测试我们看不到的开关;并显示一堆与杀死进程无关的UI内容。



我建议你坚持实际杀死你想要的明确(使用硬代码),然后通过所有选择旋转使其成为通用...一旦你实际查杀了。





如果(chkCloseLabware.Checked == true)

{

if(chkExit.Checked == true)

{

KillLabWare();

关闭();

}

其他

{

KillLabWare();

}

}
You're testing switches we can't see; and show a bunch of UI stuff not related to "killing a process".

I suggest you stick to actually killing just what you want "explicitly" (using "hard code"), then make it generic with all the selection gyrations ... once you have the actually killing figured out.


if (chkCloseLabware.Checked == true)
{
if (chkExit.Checked == true)
{
KillLabWare();
Close();
}
else
{
KillLabWare();
}
}


这篇关于如何仅断开与LOCAL远程应用程序的连接,其次如何仅在LOCAL远程桌面上退出应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆