Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:27:03+00:00 2026-06-11T15:27:03+00:00

In this case its not working good it keep adding the same links to

  • 0

In this case its not working good it keep adding the same links to the List.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Net;
using System.Web;


namespace GatherLinks
{
    public partial class Form1 : Form
    {
        int sites = 0;
        int y = 0;
        string url = @"http://www.google.co.il";
        string guys = "http://www.google.com";

        public Form1()
        {
            InitializeComponent();

            List<string> a = webCrawler(guys, 2);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private int factorial(int n)
        {
            if (n == 0) return 1;
            else y = n * factorial(n - 1);
            richTextBox1.Text = y.ToString();
            return y;


        }

        private List<string> getLinks(HtmlAgilityPack.HtmlDocument document)
        {

            List<string> mainLinks = new List<string>();
            var linkNodes = document.DocumentNode.SelectNodes("//a[@href]");
            if (linkNodes != null)
            {
                foreach (HtmlNode link in linkNodes)
                {
                    var href = link.Attributes["href"].Value;
                    mainLinks.Add(href);
                }
            }
            return mainLinks;

        }


        private List<string> webCrawler(string url, int levels)
        {

                HtmlAgilityPack.HtmlDocument doc;
                HtmlWeb hw = new HtmlWeb();
                List<string> webSites;// = new List<string>();
                List<string> csFiles = new List<string>();

                csFiles.Add("temp string to know that something is happening in level = " + levels.ToString());
                csFiles.Add("current site name in this level is : " + url);
                                try
                {
                    doc = hw.Load(url);
                    webSites = getLinks(doc);

                    if (levels == 0)
                    {
                        return csFiles;
                    }
                    else
                    {
                        int actual_sites = 0;
                        for (int i = 0; i < webSites.Count() && i < 20; i++)                         {
                            string t = webSites[i];
                                                        if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) // replace this with future FilterJunkLinks function
                            {
                               // for (int e = 0; e < csFiles.Count; e++)
                               // {
                                    if (csFiles.Contains(t))
                                    {
                                    }
                                    else
                                    {
                                        actual_sites++;
                                        csFiles.AddRange(webCrawler(t, levels - 1));
                                        Texts(richTextBox1, "Level Number " + levels + " " + t + Environment.NewLine, Color.Red);
                                    }
                               // }
                            }
                        }
                        // report to a message box only at high levels..
                        //if (levels==1)
                        //MessageBox.Show(actual_sites.ToString());

                        return csFiles;
                    }



                }
                catch
                {
                    return csFiles;
                }

        }

And the Texts function:

public void Texts(RichTextBox box, string text, Color color)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;

            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        } 

I need two things to do in the webCrawler function:

  1. If the url variable can not be resolved then the try and catch should do the job.

  2. If the List csFiles contains already the same items do not add them again. For example if in the csFiles there already http://www.google.com then do not add http://www.google.com again so in the end the csFiles List will contain http://www.google.com only once.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:27:04+00:00Added an answer on June 11, 2026 at 3:27 pm

    Your use of contains is wrong. If I understand your problem.. I will be brave and hazard a guess..

    for (int i = 0; i < webSites.Length && i < 20; i++)
    {
    
       if ((t.StartsWith("http://") == true) || (t.StartsWith("https://") == true)) // replace this with future FilterJunkLinks function
       {
         if (csFiles.Contains(t))
         {
         //dosomething
         }
         else
         {
            actual_sites++;
            csFiles.AddRange(webCrawler(t, levels - 1));
            Texts(richTextBox1, "Level Number " + levels + " " + t + Environment.NewLine,  
            Color.Red);
         }
       }
    }
    

    }

    Update:
    I don’t understand much about recursion. In fact I avoid it but I think that’s another of your problem.
    When you recursively call your code you are “forgetting” the previous list containing all the links. So instead of creating a new csFile every time I suggest you pass in a reference to the list in webcrawler. Hope that solves it.

    Meaning:

    private List<string> webCrawler(string url, int levels,List<string> csFiles)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone help me understand how to write this case statement properly its not
I swear this used to work, but it's not in this case. I'm trying
I have tried this code to copy my data to plist but its not
This is not the same old question about the validators and update panel compatibility.
I'm working on an ASP.NET MVC site, using EF4 and Automapper. I'm making good
I thought I was good at WinForms stuff but apparently that's not the case.
This case is a bit complex, I hope I will simplify it well. My
In this case the character is ^ . For example in the following format
In this case, the source of the text is from a winforms textbox. I'm
In this case, the Visual Studio designer generates a method which takes the parameter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.