Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am new in c sharp.
I have created a form that have a text box and a button. I want that when I give any web address and click the button the code generate the url of that html page.
this runs fine in console application bt now I want to creat a txt file and save the url. this code creat a file bt it too huge like 700mb and unable to open.
this is my code.
C#
WebClient client = new WebClient();
           string html = client.DownloadString(textBox1.Text);


           HtmlTag tag;

           HtmlParser.HtmlParser parse = new HtmlParser.HtmlParser(html);
           while (parse.ParseNext("a", out tag))
           {

               string value;
               if (tag.Attributes.TryGetValue("href", out value))
               {


                   fs = new FileStream("URL.txt", FileMode.Create, FileAccess.Write);
                  sw=new StreamWriter(fs);
                   while(true)
                   {
                       sw.WriteLine(value);
                   }
               }
               sw.Close();
               MessageBox.Show("Done");

           }


What mistak I am doing..
Posted

Problem I think lies in following code

C#
while(true)
{
 sw.WriteLine(value);
}


This will keep going forever, there is no break
You don't need a while loop here I guess.

Hope that helps
Milind
 
Share this answer
 
Comments
Mithlesh Shaw 4-Dec-12 8:38am    
ok.bt Can I show the url in dataGridview?
Plz provide me the code.......
MT_ 4-Dec-12 8:40am    
I think that can be a separate question and you need to elaborate more than what you have written in this comment.
If this solves your problem of text file then accept and close. Open a new question.
Manfred Rudolf Bihy 4-Dec-12 8:48am    
"Plz provide me the code......." Sorry Mitlesh, that is not how it works here on CP. We don't provide code on demand. First one tries onesself and if a failure occurs one comes back with detailed information and a proper question. Demanding code like you did is just plainly rude. Please feel free to consult the forum guidelines: FAQ
Thanks!
Manfred Rudolf Bihy 4-Dec-12 8:43am    
That's the most obvious mistake OP made! Have a 5!
You have two issues here. First you're overwriting your file for each and every link you found in the HTML and secondly you built an infinite loop writing to that file. Here is your code with the fixes:

C#
WebClient client = new WebClient();
string html = client.DownloadString(textBox1.Text);


HtmlTag tag;

HtmlParser.HtmlParser parse = new HtmlParser.HtmlParser(html);
fs = new FileStream("URL.txt", FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs);

while (parse.ParseNext("a", out tag))
{

    string value;
    if (tag.Attributes.TryGetValue("href", out value))
    {
        //while(true) // infinite loop must be removed
        //{           // you only want to write the href attribute's value once
            sw.WriteLine(value);
        //}
    }
}
// When the all anchor tags have been parsed close the file and show the "Done!" message.
sw.Close();
MessageBox.Show("Done!");


Regards,

Manfred
 
Share this answer
 
v2
Comments
Mithlesh Shaw 4-Dec-12 8:50am    
thanks
Manfred Rudolf Bihy 4-Dec-12 9:30am    
You're welcome! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900