Code download function C#

Here is a tutorial to write a function to download file and can be resumed, so we need a link and a destination where the file will be stored.

To do this task we need to use HttpWebRequest, this way we can resume download if we have any error, so we will loop until we finish the downloading process.

public void downloadFile(string _link, string destinationPath){
            bool download = true;
            long fileSize = 0;
            int bufferSize = 1024;
            long existLen = 0;
            System.IO.FileStream saveFileStream;
            System.Net.HttpWebRequest httpReq;
            System.Net.HttpWebResponse httpRes;

            while (download)
            {
                try
                {
                    fileSize = 0;
                    existLen = 0;

                    if (System.IO.File.Exists(destinationPath))
                    {
                        System.IO.FileInfo destinationFileInfo = new System.IO.FileInfo(destinationPath);
                        existLen = destinationFileInfo.Length;
                    }

                    if (existLen > 0)
                    {
                        saveFileStream = new System.IO.FileStream(destinationPath,
                                                                  System.IO.FileMode.Append,
                                                                  System.IO.FileAccess.Write,
                                                                  System.IO.FileShare.ReadWrite);
                    }
                    else
                    {
                        saveFileStream = new System.IO.FileStream(destinationPath,
                                                                  System.IO.FileMode.Create,
                                                                  System.IO.FileAccess.Write,
                                                                  System.IO.FileShare.ReadWrite);
                    }

                    httpReq = (HttpWebRequest)HttpWebRequest.Create(_link);
                    httpReq.AddRange((int)existLen);

                    System.IO.Stream resStream;
                    httpRes = (HttpWebResponse)httpReq.GetResponse();
                    resStream = httpRes.GetResponseStream();

                    fileSize = httpRes.ContentLength + existLen;

                    int byteSize;
                    byte[] downBuffer = new byte[bufferSize];

                    while ((byteSize = resStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
                    {
                        saveFileStream.Write(downBuffer, 0, byteSize);
                    }

                    saveFileStream.Close();
                    download = false;
                }catch (Exception ex){
                    download = true;
                }
            }
        }

So as you see the method is simple and can be integrated on any software, so it can be essentially used in a thread, you transform it quickly to thread and enjoy your downloader.  

0 commentaires: