메뉴 건너뛰기

Programing

C# 게시판

다운로드 폼 구현

관리자2 2019.01.02 16:29 조회 수 : 246

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace tssmart3

{

    public partial class Form1 : Form

    {

        Boolean isBusy;

        public Form1()

        {

            InitializeComponent();

        }

 

        private void btnDown_Click(object sender, EventArgs e)

        {

            if (isBusy)

            {

                webClient.CancelAsync();

                isBusy = false;

            }

            else

            {

                try

                {

                    var strFileName = this.txtUrl.Text.Split(new Char[] { '/' });

                    System.Array.Reverse(strFileName);

                    Uri uri = new Uri(this.txtUrl.Text);

                    var str = webClient.DownloadString(uri);

                    webClient.DownloadFileAsync(uri, strFileName[0]);

                    isBusy = true;

                }

                catch

                {

                    MessageBox.Show("다운로드가 실패", "Waring", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

        }

 

        private void webClient_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)

        {

            this.pgbDownload.Value = e.ProgressPercentage;

        }

 

        private void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)

        {

            isBusy = false;

            if (e.Error == null)

                MessageBox.Show("다운로드가 완료되었습니다.", "알림", MessageBoxButtons.OK, MessageBoxIcon.Information);

            else

                MessageBox.Show("다운로드 실패"+e.Error.Message.ToString());

        }

    }

}

다운로드 주소 입력후 버튼을 누르면 다운로드가 되며 진행도가 프로그래스바 로 표현 된다.
위로