tags:

views:

84

answers:

1

I am downloading a file from a website and I have the link. But the end part of the link changes to the current month. I need to loop through the link, making changes to the month part of it, and download the file.

My first question is how to know if a link has failed, to go to another link? Second ,can you can suggest me a way to loop through the link?

Initially I provide the link in a textbox.

Here is the current code

if (textBox2.Text != "")
             {
                 System.Net.WebClient we = new System.Net.WebClient();
                 we.DownloadFile(textBox2.Text, "c:\\NPPES.zip");
//this is the link                 //"http://nppesdata.cms.hhs.gov/NPPES_Data_Dissemination_July_2009.zip","c:\\NPPES.zip");
                 //string file = "C:\\NPPES.csv";
                 Unzipfile("c:\\NPPES.zip", "c:\\NPPES.csv");
                 MessageBox.Show("Download complete.Select the file to Import data");
             }
+3  A: 

if by "loop through the link" you mean generate a correct link, you can do this:

string curr_month = DateTime.Now.ToString("MMMM_yyyy",
                System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat);
string link = "http://nppesdata.cms.hhs.gov/NPPES_Data_Dissemination_" 
             + curr_month
             + ".zip";

to check if a download succeded, catch the WebException:

System.Net.WebClient we = new System.Net.WebClient();
try
{
    we.DownloadFile("", "");
}
catch (System.Net.WebException wex)
{
    //failed!
}
najmeddine
Yes, by "loop through the link" I mean generate a working link.And thanks for the reply.
shanthiram