 //MTODO PARA DESCARGAR LOS ARCHIVOS ".xml" -- PARA SOLICITAR LA DESCARGA DE CFDI
        public void SolicitaCFDI()
        {
            //try
            //{
            //    var XML = webBrowser1.Document.All.Cast<HtmlElement>().Where(htmlElement => htmlElement.Id == "BtnDescarga").ToList();
            //    toolStripProgressBar1.ProgressBar.Value = 0;
            //    toolStripProgressBar1.ProgressBar.Maximum = XML.Count;
            //    if (ContadorDescagas < XML.Count)
            //    {
            //        toolStripProgressBar1.Value = 0;
            //        WebClient wc = new WebClient();
            //        string cookie = "";
            //        uint datasize = 8192;
            //        StringBuilder cookieData = new StringBuilder((int)datasize);
            //        string dw_path = System.Convert.ToString(webBrowser1.Document.Url);

            //        wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
            //        wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            //        wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
            //        wc.DownloadFileAsync(new Uri("https://portalcfdi.facturaelectronica.sat.gob.mx/ConsultaReceptor.aspx" + XML[ContadorDescagas]), +ContadorDescagas + "*.zip");
            //        MessageBox.Show("EL TOTAL DE CFDI SON: " + XML.Count, "Descarga Masiva", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

            //        webBrowser1.Document.GetElementById("seleccionador").InvokeMember("click");
            //        webBrowser1.Document.GetElementById("ctl00$MainContent$BtnDescargar").InvokeMember("click");
            //    }
            //}

            //catch (Exception e)
            //{
            //    MessageBox.Show("Error: " + e);
            //}


            HtmlDocument htmlDocument = webBrowser1.Document;
            ////string navegar = htmlDocument.GetType.ToString();
            HtmlElementCollection htmlElementCollection = htmlDocument.Images;
            int CadIni, CadFin;    
            listBox1.Items.Clear();

            foreach (HtmlElement htmlElement in htmlElementCollection)
            {
                string imgUrl = htmlElement.GetAttribute("id");
                if (imgUrl == "BtnDescarga")
                {
                    imgUrl = Regex.Replace(htmlElement.OuterHtml, Convert.ToString((char)34), "");

                    ////EN ESTAS 2 VARIABLES LOCALIZA LA CADENA DE COMPROBANTE
                    CadIni = string.CompareOrdinal (imgUrl, "?Datos=");
                    CadFin = string.CompareOrdinal (imgUrl, "','Recuperacion')");

                    //imgUrl = imgUrl.Substring(CadIni, (CadFin - CadIni) - 0);
                    imgUrl = "https://portalcfdi.facturaelectronica.sat.gob.mx/RecuperaCfdi.aspx?" + imgUrl;
                    ////GUARDA LA CADENA EN UN listBox1 OCULTO EN EL webBrowser
                    listBox1.Items.Add(imgUrl);
                }
            }

            string URI = "https://portalcfdi.facturaelectronica.sat.gob.mx/RecuperaCfdi.aspx?";        
            int archivo = 0;
            WebClient wc = new WebClient();
            wc.Headers.Add(HttpRequestHeader.Cookie, GetCookieString(webBrowser1.Document.Url.ToString()));

            ////RECORRO EL LISTBOX Y DESCARGA 1x1
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                archivo = i + 1;
                URI = listBox1.Items[i].ToString().Trim();
               // wc.DownloadFile(URI, toolStripTextBox1.Text.Trim() + @"\" + archivo.ToString() + ".xml");
                wc.DownloadFile(URI, " " + archivo.ToString() + ".xml");
            }

            ////RENOMBRE ARCHIVOS XML CON EL FOLIO FISCAL
            string renombrar = " ";
            string foliofiscal = " ";
            DirectoryInfo folder = new DirectoryInfo(toolStripTextBox1.Text.Trim());

            foreach (FileInfo file in folder.GetFiles())
            {
                renombrar = toolStripTextBox1.Text.Trim() + @"\" + file.Name;
                XmlTextReader reader = new XmlTextReader(renombrar);
                while ((reader.Read()))
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element: //MOSTRAR COMIENZO DEL ELEMENTO
                            if (reader.HasAttributes) //CHECA SI LOS ATRIBUTOS EXISTEN
                            { 
                                while (reader.MoveToNextAttribute())
                                {
                                    ////OBTIENE VALOR Y NOMBRE DEL ATRIBUTO
                                    if (reader.Name == "UUID")
                                    {
                                        foliofiscal = reader.Value;
                                    }
                                }
                            }
                            break;
                    }
                }

                reader.Close(); //CIERRA LA LECTURA DE LOS ARCHIVOS

                ////VERIFICA SI EXISTE EL ARCHIVO O SINO LO RENOMBRA
                if (File.Exists(toolStripTextBox1.Text.Trim() + @"\" + foliofiscal + ".xml") == false)
                {
                    if (!File.Exists(@"\" + foliofiscal + ".xml"))
                    {
                        File.Move(@"\" + foliofiscal + ".xml", @"\" + foliofiscal + ".xml");
                    }
                    else
                    {
                        MessageBox.Show("YA EXISTE EL ARCHIVO", "Descarga Masiva", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                    }
                    MessageBox.Show("NO SE ENCONTR EL ARCHIVO", "Descarga Masiva", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                }
            }
            MessageBox.Show("SE DESCARGARON " + archivo + "ARCHIVOS EN LA CARPETA [" + toolStripTextBox1.Text.Trim() + "]", "Descarga Masiva", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }


        //MTODO PARA LA DESCARGA DE CFDI
        public void DescargarCFDI()
        {
            var XML = webBrowser1.Document.All.Cast<HtmlElement>().Where(htmlElement => htmlElement.Id == "BtnDescarga").ToList();
            toolStripProgressBar1.ProgressBar.Value = 0;
            toolStripProgressBar1.ProgressBar.Maximum = XML.Count;
            if (ContadorDescagas < XML.Count)
            {
                toolStripProgressBar1.Value = 0;
                WebClient wc = new WebClient();
                string cookie = "";
                uint datasize = 8192;
                StringBuilder cookieData = new StringBuilder((int)datasize);
                string dw_path = System.Convert.ToString(webBrowser1.Document.Url);

                wc.Headers.Add(HttpRequestHeader.Cookie, cookie);
                wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
                wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
                wc.DownloadFileAsync(new Uri("https://portalcfdi.facturaelectronica.sat.gob.mx/ConsultaDescargaMasiva.aspx" + XML[ContadorDescagas]), +ContadorDescagas + "*.zip");
                MessageBox.Show("EL TOTAL DE CFDI SON: " + XML.Count, "Descarga Masiva", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);

                webBrowser1.Document.GetElementById("ContenedorDinamico").InvokeMember("click");
                webBrowser1.Document.GetElementById("BtnDescarga").InvokeMember("click");
            }
        }


	 //BOTN DE DESCARGA
        private void toolStripButton3_Click_1(object sender, EventArgs e)
        {
            //LLAMAMOS A LOS MTODOS
            var uri = new Uri("https://portalcfdi.facturaelectronica.sat.gob.mx/ConsultaReceptor.aspx");
            if (webBrowser1.Url == uri)
            {
                SolicitaCFDI();
            }
            else
            {
                DescargarCFDI();
            }
        }
