添加启动进程(RDP)的富文本框的链接

时间:2013-05-08 01:56:55

标签: c# label richtextbox rdp

我正在尝试将一个可点击的链接添加到富文本框中,其中显示的内容类似于“启动RDP”,因此当用户点击它时,它将启动Windows远程桌面并使用将机器名称放在框中为您。这是我的代码到目前为止,它在Active Directory中搜索用户输入的计算机名称,ping机器以及是否在线,在另一个文本框中显示其状态。

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

有什么建议吗?我尝试过一些不同的东西,但似乎无法让它发挥作用。

谢谢!

1 个答案:

答案 0 :(得分:0)

在Codeproject上发表了一篇文章,描述了您正在寻找的解决方案:link

希望这会有所帮助。 Rgds,AB