0

我正在尝试向富文本框添加一个可点击的链接,上面写着“启动 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;
    }

有什么建议么 ?我尝试了一些不同的方法,但似乎无法使其正常工作。

谢谢 !

4

1 回答 1

0

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

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

于 2013-05-08T02:02:17.103 回答