Пример #1
0
 public static void Setup()
 {
     DirectoryManager.Create(PATH);
 }
Пример #2
0
        public override TaskResultType Execute(DownloadRequestSession objDownloadRequestSession)
        {
            TaskResultType enuTaskResult = TaskResultType.Completed;

            objDownloadRequestSession.UpdateProgess();

            HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(base.Source);

            if (base.Credentials != null)
            {
                objWebRequest.Credentials = base.Credentials.CreateNetworkCredentials();
            }
            objWebRequest.Timeout = base.TimeOut;
            objWebRequest.Proxy   = ProxySettings.DefaultProxy;

            using (HttpWebResponse objWebResponse = (HttpWebResponse)objWebRequest.GetResponse())
            {
                objDownloadRequestSession.DownloadStats.Size = objWebResponse.ContentLength;
                objDownloadRequestSession.UpdateProgess();

                bool blnFileExists = FileManager.Exists(base.Destination);
                if (blnFileExists == true)
                {
                    FileManager.Delete(base.Destination);
                }

                string strDirectory       = Path.GetDirectoryName(base.Destination);
                bool   blnDirectoryExists = DirectoryManager.Exists(strDirectory);
                if (blnDirectoryExists == false)
                {
                    DirectoryManager.Create(strDirectory);
                }

                using (Stream objResponseStream = objWebResponse.GetResponseStream())
                {
                    using (FileStream objFileStream = new FileStream(base.Destination, FileMode.Create, FileAccess.Write))
                    {
                        byte[] bytBuffer    = new byte[57344];
                        int    intBytesRead = objResponseStream.Read(bytBuffer, 0, bytBuffer.Length);

                        while ((intBytesRead != 0) && (objDownloadRequestSession.JobTicket.Cancelled == false))
                        {
                            objFileStream.Write(bytBuffer, 0, intBytesRead);
                            objFileStream.Flush();

                            objDownloadRequestSession.DownloadStats.BytesReceived += intBytesRead;
                            objDownloadRequestSession.UpdateProgess();

                            intBytesRead = objResponseStream.Read(bytBuffer, 0, bytBuffer.Length);
                        }
                    }
                }

                objDownloadRequestSession.DownloadStats.EndTime = DateTime.Now;
                objDownloadRequestSession.UpdateProgess();

                if (objDownloadRequestSession.JobTicket.Cancelled == true)
                {
                    FileManager.Delete(base.Destination);
                }
            }

            enuTaskResult = ((objDownloadRequestSession.JobTicket.Cancelled == true) ? TaskResultType.Cancelled : TaskResultType.Completed);

            return(enuTaskResult);
        }