Esempio n. 1
0
        private void UpdateSessions()
        {
            var currentServer = TerminalServerHandle.GetCurrent();

            usersMenuItem.MenuItems.Clear();

            foreach (var session in currentServer.GetSessions())
            {
                string displayName = session.DomainName + "\\" + session.UserName;

                if (displayName == "\\")
                {
                    // Probably the Services or RDP-Tcp session.
                    session.Dispose();
                    continue;
                }

                MenuItem userMenuItem = new MenuItem();

                userMenuItem.Text = session.SessionId + ": " + displayName;

                MenuItem currentMenuItem;

                currentMenuItem = new MenuItem() { Text = "Disconnect", Tag = session.SessionId };
                currentMenuItem.Click += (sender, e) =>
                    {
                        int sessionId = (int)((MenuItem)sender).Tag;

                        SessionActions.Disconnect(this, sessionId, false);
                    };
                userMenuItem.MenuItems.Add(currentMenuItem);
                currentMenuItem = new MenuItem() { Text = "Logoff", Tag = session.SessionId };
                currentMenuItem.Click += (sender, e) =>
                {
                    int sessionId = (int)((MenuItem)sender).Tag;

                    SessionActions.Logoff(this, sessionId, true);
                };
                userMenuItem.MenuItems.Add(currentMenuItem);
                currentMenuItem = new MenuItem() { Text = "Send Message...", Tag = session.SessionId };
                currentMenuItem.Click += (sender, e) =>
                {
                    int sessionId = (int)((MenuItem)sender).Tag;

                    try
                    {
                        var mbw = new MessageBoxWindow();

                        mbw.MessageBoxTitle = "Message from " + Program.CurrentUsername;
                        mbw.OkButtonClicked += () =>
                            {
                                try
                                {
                                    TerminalServerHandle.GetCurrent().GetSession(sessionId).SendMessage(
                                        mbw.MessageBoxTitle,
                                        mbw.MessageBoxText,
                                        MessageBoxButtons.OK,
                                        mbw.MessageBoxIcon,
                                        0,
                                        0,
                                        mbw.MessageBoxTimeout,
                                        false
                                        );
                                    return true;
                                }
                                catch (Exception ex)
                                {
                                    PhUtils.ShowException("Unable to send the message", ex);
                                    return false;
                                }
                            };
                        mbw.ShowDialog();
                    }
                    catch (Exception ex)
                    {
                        PhUtils.ShowException("Unable to show the message window", ex);
                    }
                };
                userMenuItem.MenuItems.Add(currentMenuItem);
                currentMenuItem = new MenuItem() { Text = "Properties...", Tag = session.SessionId };
                currentMenuItem.Click += (sender, e) =>
                {
                    int sessionId = (int)((MenuItem)sender).Tag;

                    try
                    {
                        var sessionInformationWindow =
                            new SessionInformationWindow(TerminalServerHandle.GetCurrent().GetSession(sessionId));

                        sessionInformationWindow.ShowDialog();
                    }
                    catch (Exception ex)
                    {
                        PhUtils.ShowException("Unable to show session properties", ex);
                    }
                };
                userMenuItem.MenuItems.Add(currentMenuItem);

                usersMenuItem.MenuItems.Add(userMenuItem);
                session.Dispose();
            }
        }
        private void UpdateSessions()
        {
            TerminalServerHandle currentServer = TerminalServerHandle.GetCurrent();

            usersToolStripMenuItem.DropDownItems.Clear();

            foreach (var session in currentServer.GetSessions())
            {
                string displayName = session.DomainName + "\\" + session.UserName;

                if (displayName == "\\")
                {
                    // Probably the Services or RDP-Tcp session.
                    session.Dispose();
                    continue;
                }

                AddMenuItemDelegate addMenuItem = (text, onClick) =>
                {
                    usersToolStripMenuItem.DropDownItems.Add(text, null, onClick);
                    shutdownTrayMenuItem.DropDownItems.Add(text, null, onClick);
                    shutDownToolStripMenuItem.DropDownItems.Add(text, null, onClick);
                };

                addMenuItem("Disconnect", (sender, e) =>
                {
                    int sessionId = (int)((MenuItem)sender).Tag;

                    SessionActions.Disconnect(this, session.SessionId, false);
                });

                //MenuItem userMenuItem = new MenuItem();
                //userMenuItem.Text = session.SessionId + ": " + displayName;
                addMenuItem("Logoff", (sender, e) =>
                {
                    int sessionId = (int)((MenuItem)sender).Tag;

                    SessionActions.Logoff(this, session.SessionId, true);
                });

                addMenuItem("Send Message...", (sender, e) =>
                {
                    try
                    {
                        MessageBoxWindow mbw = new MessageBoxWindow
                        {
                            MessageBoxTitle = "Message from " + Program.CurrentUsername
                        };

                        mbw.OkButtonClicked += () =>
                        {
                            try
                            {
                                TerminalServerHandle.GetCurrent().GetSession(session.SessionId).SendMessage(
                                    mbw.MessageBoxTitle,
                                    mbw.MessageBoxText,
                                    MessageBoxButtons.OK,
                                    mbw.MessageBoxIcon,
                                    0,
                                    0,
                                    mbw.MessageBoxTimeout,
                                    false
                                    );

                                return true;
                            }
                            catch (Exception ex)
                            {
                                PhUtils.ShowException("Unable to send the message", ex);
                                return false;
                            }
                        };
                        mbw.ShowDialog();
                    }
                    catch (Exception ex)
                    {
                        PhUtils.ShowException("Unable to show the message window", ex);
                    }
                });

                addMenuItem("Properties...", (sender, e) =>
                {
                    try
                    {
                        using (var sessionWindow = new SessionInformationWindow(TerminalServerHandle.GetCurrent().GetSession(session.SessionId)))
                            sessionWindow.ShowDialog();
                    }
                    catch (Exception ex)
                    {
                        PhUtils.ShowException("Unable to show session properties", ex);
                    }
                });

                session.Dispose();
            }
        }