Pregunta

Estoy descargando un archivo de un sitio web y tengo el enlace. Pero la parte final del enlace cambia al mes actual. Necesito recorrer el enlace, hacer cambios en la parte del mes y descargar el archivo.

Mi primera pregunta es cómo saber si un enlace ha fallado, ¿para ir a otro enlace? En segundo lugar, ¿puede sugerirme una forma de recorrer el enlace?

Inicialmente proporciono el enlace en un cuadro de texto.

Aquí está el código actual

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");
             }
¿Fue útil?

Solución

si por " recorre el enlace " te refieres a generar un enlace correcto, puedes hacer esto:

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";

para verificar si una descarga se realizó correctamente, capture la WebException :

System.Net.WebClient we = new System.Net.WebClient();
try
{
    we.DownloadFile("", "");
}
catch (System.Net.WebException wex)
{
    //failed!
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top