সি শার্প প্রোজেক্ট [পর্ব-০৫] :: তৈরি করুন নিজের ডাউনলোডার সুপার এবং ফাস্ট ডাউনলোডার

সি শার্প প্রোজেক্ট

প্রিয় সি শার্প শিক্ষার্থীবৃন্দ আজ আমরা এমন একটি প্রোগ্রাম তৈরি করব যা আমাদের আধুনিক জুগে বেশ প্রয়োজন। আমরা ইন্টারনেট থেকে কত কিছুই না ডাউনলোড করি। সব কিছুই ডাউনলোড করি অপরের তৈরি করা ডাউনলোডার দিয়ে। আজ থেকে আমরা ডাউনলোড করবো নিজের তৈরি ডাউনলোডার দিয়ে।
চলুন তাহলে শুরু করা যাক। প্রথমে visual studio 2008 open করি।
তারপর New > Project

এখন একটি বক্স ওপেন হবে। বাম পাশের বক্স থেকে visual c# > windows
তারপর ডানপাশের বক্স থেকে windows forms application select  করি। তারপর প্রোজেক্ট এর নাম লিখুন।

এবার ok ক্লিক করুন। এখন ফর্ম ওপেন হবে

আখন আমরা এই ফরম এ ডিজাইন করবো। প্রয়োজনীয় টুলস যোগ করবো। এখন টুলস বক্স থেকে ৩ টি লেভেল, ৩ টি বাটন, ১ টি প্রচেসবার এবং ২ টি টেক্সট বক্স নেই এখন আমরা নিচের মত টেক্সট বক্স ও বাটন এর নাম প্ররিবরতন করবো Textbox 1 = txtUrl, Textbox 2 = txtPath, txtPath টেক্সট ইভেন্ট এ লিখি G:\Filename.mp3

এখানে G:\  যে ড্রাইভ এ ডাটা সেভ হবে। Filename ফাইল এর নাম লিখতে হবে। .mp3 যে ধরনের ফাইল নামবে। এখানে .exe ফাইল নামাতে হলে .mp3 এর পরিবর্তে .exe লিখতে হবে। এবার বাটন গুলর নাম ও টেক্সট পরিবর্তন করবো

Button
Button name
Button text
Button1
btnDownload
Download
Button2
btnPauseResume
Pause
Button3
btnStop
Stop
এখন আমরা লেভেল গুলর টেক্সট পরিবর্তন করবো
Level Name
Level name
Level text
label1
lblUrl
Download URL:
Label2
lblPath
Download Path:
Label3
lblProgress
A waiting Download
এখন progressBar1 এর নাম পরিবর্তন করে লিখি prgDownload।
এখন একটু ডিজাইন করলে ফরম দেখতে নিচের ছবির মত হবে।
এখন আমরা কোডিং করবো প্রথমে ফরম এর উপর রাইট বাটন ক্লিক করে view code এ click করবো। এখন আমাদের সামনে কোড উইন্ডো ওপেন হবে। এখন আমরা নিচের কোড গুলো কপি করে কোড উইন্ডো এর ভিতর
</pre>
</div>
<div>
<div>using System;</div>
<div>using System.Collections.Generic;</div>
<div>using System.ComponentModel;</div>
<div>using System.Data;</div>
<div>using System.Drawing;</div>
<div>using System.Text;</div>
<div>using System.Windows.Forms;</div>
<div>using System.Net;</div>
<div>using System.IO;</div>
<div>using System.Threading;</div>

এই কোড গুলো দিয়ে

<div>using System;</div>
<div>using System.Collections.Generic;</div>
<div>using System.ComponentModel;</div>
<div>using System.Data;</div>
<div>using System.Drawing;</div>
<div>using System.Text;</div>
<div>using System.Windows.Forms;</div>
<div>
গুলকে replace করুন।
তারপর
        public Form2()
        {
            InitializeComponent();
        }
এই কোড এর নিচে
</pre>
</div>
<div>private Thread thrDownload;</div>
<div>private Stream strResponse;</div>
<div>private Stream strLocal;</div>
<div>private HttpWebRequest webRequest;</div>
<div>private HttpWebResponse webResponse;</div>
<div>private static int PercentProgress;</div>
<div>private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);</div>
<div>bool goPause = false;</div>
<div>        private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)</div>
<div>        {</div>
<div>            // Calculate the download progress in percentages</div>
<div>            PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);</div>
<div>            //convert kb to mb</div>
<div>            BytesRead = BytesRead / 1048576;</div>
<div>            //convert kb to gb</div>
<div>            TotalBytes = TotalBytes / 1048576;</div>
<div>            // Make progress on the progress bar</div>
<div>            prgDownload.Value = PercentProgress;</div>
<div>            // Display the current progress on the form</div>
<div></div>
<div>            if (BytesRead >= 1024 || TotalBytes >= 1024)</div>
<div>            {</div>
<div>                lblProgress.Text = "Downloaded " + BytesRead + " GB - out of " + TotalBytes + " GB -- (" + PercentProgress + "%)";</div>
<div>            }</div>
<div>            else</div>
<div>            {</div>
<div>                lblProgress.Text = "Downloaded " + BytesRead + " MB - out of " + TotalBytes + " MB -- (" + PercentProgress + "%)";</div>
<div>            }</div>
<div>        }</div>
<div></div>
<div></div>
<div>  private void Download(object startPoint)</div>
<div>        {</div>
<div>            try</div>
<div>            {</div>
<div>                // Put the object argument into an int variable</div>
<div>                int startPointInt = Convert.ToInt32(startPoint);</div>
<div>                // Create a request to the file we are downloading</div>
<div>                webRequest = (HttpWebRequest)WebRequest.Create(txtUrl.Text);</div>
<div>                // Set the starting point of the request</div>
<div>                webRequest.AddRange(startPointInt);</div>
<div>                Timer1.Enabled = true;</div>
<div></div>
<div>                // Set default authentication for retrieving the file</div>
<div>                webRequest.Credentials = CredentialCache.DefaultCredentials;</div>
<div>                if (webRequest==null)</div>
<div>                {</div>
<div>                    MessageBox.Show("The File can not downloaded.", "Could not Download", MessageBoxButtons.OK, MessageBoxIcon.Error);</div>
<div></div>
<div>                }</div>
<div>                // Retrieve the response from the server</div>
<div>                webResponse = (HttpWebResponse)webRequest.GetResponse();</div>
<div></div>
<div>                // Ask the server for the file size and store it</div>
<div>                Int64 fileSize = webResponse.ContentLength;</div>
<div></div>
<div>                // Open the URL for download</div>
<div>                strResponse = webResponse.GetResponseStream();</div>
<div></div>
<div>                // Create a new file stream where we will be saving the data (local drive)</div>
<div>                if (startPointInt == 0)</div>
<div>                {</div>
<div>                    strLocal = new FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None);</div>
<div></div>
<div>                }</div>
<div>                else</div>
<div>                {</div>
<div>                    strLocal = new FileStream(txtPath.Text, FileMode.Append, FileAccess.Write, FileShare.None);</div>
<div></div>
<div>                }</div>
<div></div>
<div>                // It will store the current number of bytes we retrieved from the server</div>
<div>                int bytesSize = 0;</div>
<div>                // A buffer for storing and writing the data retrieved from the server</div>
<div>                byte[] downBuffer = new byte[2048];</div>
<div></div>
<div>                // Loop through the buffer until the buffer is empty</div>
<div>                while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)</div>
<div>                {</div>
<div>                    // Write the data from the buffer to the local hard drive</div>
<div>                    strLocal.Write(downBuffer, 0, bytesSize);</div>
<div></div>
<div>                    // Invoke the method that updates the form's label and progress bar</div>
<div>                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt });</div>
<div></div>
<div>                    if (goPause == true)</div>
<div>                    {</div>
<div>                        break;</div>
<div>                    }</div>
<div>                }</div>
<div></div>
<div>            }</div>
<div>            finally</div>
<div>            {</div>
<div>                // When the above code has ended, close the streams</div>
<div>                strResponse.Close();</div>
<div>                strLocal.Close();</div>
<div>            }</div>
<div>        }</div>
<div>
এই কোড পেস্ট করবো নিচের ছবির মত
 এখন Download বাটন এ ডাবল ক্লিক করে
        private void btnDownload_Click(object sender, EventArgs e)
        {
          এইখানে নিচের কোড গুলো পেস্ট করি।
  }
কোড গুলো
</pre>
</div>
<div>if (thrDownload != null && thrDownload.ThreadState == ThreadState.Running)</div>
<div>            {</div>
<div>                MessageBox.Show("A download is already running. Please either the stop the current download or await for its completion before starting a new one.", "Download in progress", MessageBoxButtons.OK, MessageBoxIcon.Error);</div>
<div>            }</div>
<div>            else</div>
<div>            {</div>
<div>                Timer1.Enabled = true;</div>
<div>                // Let the user know we are connecting to the server</div>
<div>                lblProgress.Text = "Download Starting";</div>
<div>                // Create a new thread that calls the Download() method</div>
<div>                thrDownload = new Thread(new ParameterizedThreadStart(Download));</div>
<div>                // Start the thread, and thus call Download(); start downloading from the beginning (0)</div>
<div>                thrDownload.Start(0);</div>
<div>                // Enable the Pause/Resume button</div>
<div>                btnPauseResume.Enabled = true;</div>
<div>                // Set the button's caption to Pause because a download is in progress</div>
<div>                btnPauseResume.Text = "Pause";</div>
<div>                btnStop.Text = "Stop";</div>
<div>                btnDownload.Enabled = false;</div>
<div>            }</div>
<div>
এভাবেই btnPauseResume বাটন এ ডাবল ক্লিক করে দুই ব্রাকেট এর মাঝে নিচের কোড গুলো পেস্ট করি
</pre>
</div>
<div>// If the thread exists</div>
<div>            if (thrDownload != null)</div>
<div>            {</div>
<div>                if (btnPauseResume.Text == "Pause")</div>
<div>                {</div>
<div>                    // The Pause/Resume button was set to Pause, thus pause the download</div>
<div>                    goPause = true;</div>
<div>                    Timer1.Enabled = false;</div>
<div>                    Label7.Location = new Point(520, 82);</div>
<div>                    // Now that the download was paused, turn the button into a resume button</div>
<div>                    btnPauseResume.Text = "Resume";</div>
<div></div>
<div></div>
<div>                    // Close the web response and the streams</div>
<div>                    webResponse.Close();</div>
<div>                    strResponse.Close();</div>
<div>                    strLocal.Close();</div>
<div>                    // Abort the thread that's downloading</div>
<div>                    thrDownload.Abort();</div>
<div>                }</div>
<div>                else</div>
<div>                {</div>
<div>                    // The Pause/Resume button was set to Resume, thus resume the download</div>
<div>                    goPause = false;</div>
<div>                    btnPauseResume.Text = "Pause";</div>
<div>                    Timer1.Enabled = true;</div>
<div>                    // Now that the download was resumed, turn the button into a pause button</div>
<div></div>
<div>                    long startPoint = 0;</div>
<div></div>
<div>                    if (File.Exists(txtPath.Text))</div>
<div>                    {</div>
<div>                        startPoint = new FileInfo(txtPath.Text).Length;</div>
<div>                    }</div>
<div>                    else</div>
<div>                    {</div>
<div>                        MessageBox.Show("The file you choosed to resume doesn't exist.", "Could not resume", MessageBoxButtons.OK, MessageBoxIcon.Error);</div>
<div></div>
<div>                        btnPauseResume.Enabled = false;</div>
<div>                    }</div>
<div></div>
<div>                    // Let the user know we are connecting to the server</div>
<div>                    lblProgress.Text = "Download Resuming";</div>
<div>                    Timer1.Enabled = true;</div>
<div>                    // Create a new thread that calls the Download() method</div>
<div>                    thrDownload = new Thread(new ParameterizedThreadStart(Download));</div>
<div>                    // Start the thread, and thus call Download()</div>
<div>                    thrDownload.Start(startPoint);</div>
<div>                    // Enable the Pause/Resume button</div>
<div>                    btnPauseResume.Enabled = true;</div>
<div>                }</div>
<div>            }</div>
<div>            else</div>
<div>            {</div>
<div>                MessageBox.Show("A download does not appear to be in progress.", "Could not pause", MessageBoxButtons.OK, MessageBoxIcon.Error);</div>
<div>                btnPauseResume.Enabled = false;</div>
<div>            }</div>
<div>
এভাবেই btnStop বাটন এ ডাবল ক্লিক করে দুই ব্রাকেট এর মাঝে নিচের কোড গুলো পেস্ট করি
</pre>
</div>
<div>if (btnStop.Text == "Stop")</div>
<div>            {</div>
<div>                btnDownload.Enabled = true;</div>
<div>                btnStop.Text = "Exit";</div>
<div>                Timer1.Enabled = false;</div>
<div>                Label7.Location = new Point(520, 82);</div>
<div>                // Abort the thread that's downloading</div>
<div>                thrDownload.Abort();</div>
<div>                // Close the web response and the streams</div>
<div>                webResponse.Close();</div>
<div>                strResponse.Close();</div>
<div>                strLocal.Close();</div>
<div>                // Set the progress bar back to 0 and the label</div>
<div>                prgDownload.Value = 0;</div>
<div>                lblProgress.Text = "Download Stopped";</div>
<div>                // Disable the Pause/Resume button because the download has ended</div>
<div>                btnPauseResume.Enabled = false;</div>
<div>            }</div>
<div>            else</div>
<div>            {</div>
<div>                Application.Exit();</div>
<div>            }</div>
<div>
এখন কীবোর্ড থেকে F5 বাটন প্রেস করে প্রোগ্রাম টি চালু করি।
ধন্যবাদ সবাইকে। আপ্নারা যদি কোন টিউমেন্ট বা লাইক না দেন তবে উতসহ পাইনা।
 প্রথম প্রকাশিত এখানে

Level 2

আমি ইসলাম আমিনুল। বিশ্বের সর্ববৃহৎ বিজ্ঞান ও প্রযুক্তির সৌশল নেটওয়ার্ক - টেকটিউনস এ আমি 12 বছর 6 মাস যাবৎ যুক্ত আছি। টেকটিউনস আমি এ পর্যন্ত 32 টি টিউন ও 27 টি টিউমেন্ট করেছি। টেকটিউনসে আমার 4 ফলোয়ার আছে এবং আমি টেকটিউনসে 0 টিউনারকে ফলো করি।


টিউনস


আরও টিউনস


টিউনারের আরও টিউনস


টিউমেন্টস