Search code examples
c#labelrichtextboxrdp

Add link to rich text box that starts a process (RDP)


I'm trying to add a clickable link to a rich text box that says something like "Start RDP" so when a user clicks on it, it will start windows remote desktop and use put the machine name in the box for you. Here is my code so far, It searchs Active Directory for a computer name that the user enters, pings the machine and if it is online, show its status in another textbox.

private void btnAd_Click(object sender, EventArgs e)
    {
        var adsb = new StringBuilder();
        var mssb = new StringBuilder();
        DirectoryEntry de = new DirectoryEntry();
        de.Path = "LDAP://dc=Domain.org";

        try
        {
            string wildcard = "*";
            string adser = wildcard + txtAd.Text + wildcard;
            DirectorySearcher ser = new DirectorySearcher();
            ser.SizeLimit = System.Int32.MaxValue;
            ser.PageSize = System.Int32.MaxValue;
            ser.Filter = "(&(ObjectCategory=computer)(name=" + adser + "))"; //Only allows Computers to be returned in results.
            SearchResultCollection results = ser.FindAll();

            if (String.IsNullOrEmpty(txtcomputers.Text)) //if the textbox is empty, write ad search results to textbox
            {
                foreach (SearchResult res in results)
                {
                    string[] temp = res.Path.Split(','); //temp[0] would contain the computer name ex: cn=computerName,..

                    adsb.AppendLine(temp[0].Substring(10));//returns everything after LDAP://CN= until end of temp[0].

                    if (Ping(temp[0].Substring(10)))
                    {
                        mssb.AppendLine(temp[0].Substring(10) + "....Online");
                    }
                    else
                    {
                        mssb.AppendLine(temp[0].Substring(10) + "....Offline");
                    }

                }

                rtbComputerstatus.Text = mssb.ToString();
                txtcomputers.Text = adsb.ToString();


            }

            else //Add items to textbox if there are already items present. 
            {
                txtcomputers.AppendText(Environment.NewLine);
                rtbComputerstatus.AppendText(Environment.NewLine);

                foreach (SearchResult res in results)
                {
                    string[] temp = res.Path.Split(',');
                    adsb.AppendLine(temp[0].Substring(10));
                    txtcomputers.AppendText(adsb.ToString());

                    if (Ping(temp[0].Substring(10)))
                    {


                        mssb.AppendLine(temp[0].Substring(10) + "....Online......");
                    }
                    else
                    {
                        mssb.AppendLine(temp[0].Substring(10) + "....Offline");
                    }

                    rtbComputerstatus.AppendText(mssb.ToString());
                }
            }

            //trims spaces 
            this.txtcomputers.Text = this.txtcomputers.Text.Trim();
            txtcomputers.CharacterCasing = CharacterCasing.Upper;

            //color items

            HighlightPhrase(rtbComputerstatus, "Online",Color.Green);
            HighlightPhrase(rtbComputerstatus, "Offline", Color.Red);
            HighlightPhrase(rtbComputerstatus, "RDP", Color.Blue);





        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }


        finally
        {

            de.Dispose();//Clean up resources
        }
    }


    // color method 

    static void HighlightPhrase(RichTextBox box, string phrase, Color color)
    {
        int pos = box.SelectionStart;
        string s = box.Text;
        for (int ix = 0; ; )
        {
            int jx = s.IndexOf(phrase, ix, StringComparison.CurrentCultureIgnoreCase);
            if (jx < 0) break;
            box.SelectionStart = jx;
            box.SelectionLength = phrase.Length;
            box.SelectionColor = color;
            ix = jx + 1;
        }
        box.SelectionStart = pos;
        box.SelectionLength = 0;
    }

 //ping method



    public static bool Ping(string hostName)
    {
        bool result = false;

        try
        {

            Ping pingSender = new Ping(); PingOptions options = new PingOptions();
            // Use the default Ttl value which is 128,

            // but change the fragmentation behavior.
            options.DontFragment = true;

            // Create a buffer of 32 bytes of data to be transmitted.
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;

            PingReply reply = pingSender.Send(hostName, timeout, buffer, options); if (reply.Status == IPStatus.Success)
            {

                result = true;
            }

            else
            {

                result = false;
            }
        }
        catch
        {
            result = false;
        }

        return result;
    }

Any suggestions ? I've tried a few different things but can't seem to get it working.

Thanks !


Solution

  • There is an article published on Codeproject, describing the solution that you are looking for: link

    Hope this will help. Rgds, AB