2013-05-08 23 views
0

我试图添加一个可点击的链接到一个类似“启动RDP”的富文本框,因此当用户点击它时,它会启动窗口远程桌面和使用使机器名在框中为您服务。这是到目前为止我的代码,它searchs Active Directory中用户输入的计算机名称,执行ping本机,如果是在网上,显示其在另一个文本状态。添加链接到启动进程的富文本框(RDP)

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

有什么建议吗?我已经尝试了一些不同的东西,但似乎无法得到它的工作。

谢谢!

回答

0

在CodeProject上发表了一篇文章,详细说明您正在寻找的解决方案:link

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

+0

我跨过来的时候我正在研究,但我不知道如何将它添加到我的代码。 – Boundinashes6 2013-05-08 03:01:50

+0

得到它的工作,感谢 – Boundinashes6 2013-05-08 05:25:11

+0

欢迎您。祝你好运! – 2013-05-08 05:30:22