Beispiel #1
0
        /// <summary>
        /// 특정 Url의 파일을 다운 받음.
        /// </summary>
        /// <param name="Url"> </param>
        public void DownloadFile(string Url, string FullPath, bool Overwrite)
        {
            HttpWebRequest  webreq = null;
            HttpWebResponse webres = null;

            try
            {
                webreq = (HttpWebRequest)WebRequest.Create(Url);
                webreq.ReadWriteTimeout = 10 * 1000;
                webres = (HttpWebResponse)webreq.GetResponse();
            }
            catch (Exception ex)
            {
                throw new Exception("Url: " + Url + Environment.NewLine + ex.Message, ex);
            }

            if (Overwrite)
            {
                CFile.DeleteUntilSuccess(FullPath, 1000 * 5);
            }

            using (BinaryWriter bw = new BinaryWriter(new FileStream(FullPath, FileMode.CreateNew, FileAccess.Write, FileShare.None), Encoding.UTF8))
            {
                BinaryReader br       = new BinaryReader(webres.GetResponseStream(), Encoding.UTF8);
                long         FileSize = webres.ContentLength;

                CDownloadProgressChangedEventArgs arg = new CDownloadProgressChangedEventArgs();
                arg.FullPath            = FullPath;
                arg.TotalBytesToReceive = FileSize;

                //if (this.OnStateChanged != null)
                //    this.OnStateChanged(PathFile, DownloadState.Started, FileSize);

                int    Max    = 4096;
                byte[] Buffer = new byte[Max];

                int BytesRead = 0;
                while ((BytesRead = br.Read(Buffer, 0, Max)) > 0)
                {
                    arg.BytesReceived     += BytesRead;
                    arg.ProgressPercentage = Math.Min(100, Convert.ToInt32((arg.BytesReceived / (double)arg.TotalBytesToReceive) * 100));
                    if (this.DownloadProgress != null)
                    {
                        this.DownloadProgress(this, arg);
                    }

                    bw.Write(Buffer, 0, BytesRead);
                }
            }
        }