Get Links and the Number of Links of a Website in C#

This is a simple tutorial on getting the links and the number of links of a website. With a link, users can navigate to another page, window, or Help topic; display a definition; initiate a command; or choose an option. A link is text or a graphic that indicates that it can be clicked, typically by being displayed using the visited or unvisited link system colors, examples are hyperlinks. 1. Let's start with creating a Windows Form Application in C# for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Add a WebBrowser named WebBrowser so that we can view its link. Now, add http://www.sourcecodester.com to the URL Property of the WebBrowser. output 3. Next, add one TextBox named TextBox1 for displaying the links of the webBrowser and a Label named Label1 for viewing the number of links for a website. Add also one Button named Button1 for getting the links and number of links of a website. Design your interface like this one: output 4.Put this code to your Button1_Click. This will trigger to get the number of links and the links of the webbrowser.
  1. private void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3.         int tempNum = default(int);
  4.         string tempString = "";
  5.         for (tempNum = 1; tempNum <= WebBrowser1.Document.Links.Count - 1; tempNum++)
  6.         {
  7.                 tempString = tempString + WebBrowser1.Document.Links(tempNum).InnerHtml + "\r\n";
  8.         }
  9.         TextBox1.Text = tempString;
  10.         Label1.Text = WebBrowser1.Document.Links.Count + " links.";
  11. }

Explanation:

We have initialized tempNum as Integer and tempString as String as equal to nothing. Next we have created a for next loop that the tempNum variable will start a value of 1 up to the number of links of the WebBroswer minus the value of 1. The tempString now will be equal to the source of the WebBrowser that also contains the number of links held by the tempNum variable. "\r\n" here is for the next line. Now, we passed the value of our links (tempString variable) to TextBox1. and the number of links will now be displayed in our Label1.

Output:

Loading of the Sourcecodester site from the WebBrowser. output Click the Button to get the links and the number of links from the Sourcecodester website. output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment