Beispiel #1
0
        static bool DownloadUpdate(string Origem, string Destino)
        {
            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(Destino, FileMode.Create,
                                            FileAccess.Write, FileShare.None);

                WebClient wc = new WebClient();
                Stream    st = wc.OpenRead(Origem);

                decimal len = Cnv.ToDecimal(wc.ResponseHeaders["Content-Length"].ToString());

                decimal totalSize  = 0;
                int     bytesSize  = 0;
                byte[]  downBuffer = new byte[2048];

                int Tentativas = 0;
                while (totalSize < len)
                {
                    bytesSize = st.Read(downBuffer, 0, downBuffer.Length);
                    if (bytesSize == 0)
                    {
                        Tentativas++;
                        if (Tentativas >= 10)
                        {
                            break;
                        }
                    }

                    fileStream.Write(downBuffer, 0, bytesSize);
                    totalSize += bytesSize;
                    int Perc = Cnv.ToInt((totalSize / len) * 100);
                    FormInfo.setProgress(Perc);
                    FormInfo.setInfo("Baixando " + (totalSize / 1000000).ToString("0.000") + " de " + (len / 1000000).ToString("0.000") + " Mb. " + Perc.ToString("0") + "%");

                    if (FormInfo.PodeCancelar)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                (new lib.Class.Log(lib.Visual.Functions.GetDirAppCondig() + "\\LogError.log")).Save(ex);
                return(false);
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close();
                }
            }
        }