Click here to Skip to main content
12,630,137 members (28,664 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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.
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 4-Dec-12 3:22am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Problem I think lies in following code

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
  Permalink  
Comments
Mithlesh Shaw 4-Dec-12 8:38am
   
ok.bt Can I show the url in dataGridview?
Plz provide me the code.......
Milind_T 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 R. 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 R. Bihy 4-Dec-12 8:43am
   
That's the most obvious mistake OP made! Have a 5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:

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
  Permalink  
v2
Comments
Mithlesh Shaw 4-Dec-12 8:50am
   
thanks
Manfred R. 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161205.3 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100