C#
2014.09.11 14:52

Download Files from Web [C#]

조회 수 9344 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print
?

단축키

Prev이전 문서

Next다음 문서

+ - Up Down Comment Print

http://www.csharp-examples.net/download-files/



Download File Synchronously

The following code shows how to download file synchronously. This method blocks the main thread until the file is downloaded or an error occur (in this case the WebException is thrown).


[C#]

using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");


Download File Asynchronously

To download file without blocking the main thread use asynchronous method DownloadFileA­sync. You can also set event handlers to show progress and to detect that the file is downloaded.



[C#]

private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
  progressBar.Value = e.ProgressPercentage;
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
  MessageBox.Show("Download completed!");
}

Note: Although you use asynchronous method, it can block the main thread for a while. It's because before the async download itself, it checks the DNS name (in this case „mysite.com“) and this check is done internally by blocking function. If you use directly IP instead of domain name, the DownloadFileAsync method will be fully asynchronous.




Dreamy의 코드 스크랩

내가 모으고 내가 보는

List of Articles
번호 분류 제목 날짜 조회 수 추천 수
401 LINUX [Shell Script] 쉘 프로그래밍을 위한 명령어 file 2015.04.30 7594 0
400 Android git archive 를 사용해서 폴더를 .git 제외하고 tar 나 zip 으로 묶기 1 2019.02.10 7647 0
399 Android JNI에서 가변 인수(varargs) 쓰기 2014.08.04 7739 0
398 LINUX 리눅스 계정 목록 보기 2016.03.21 7772 0
397 일반 VP9 코덱 2015.09.01 7813 0
396 LINUX 우분투(Ubuntu)에서 APM 웹서버 구축하기 2016.03.17 7826 0
395 Pi Simple DAC - PWM을 Voltage로 변환하는 회로 file 2016.11.21 7855 0
394 Android gdb commands 2015.03.11 7915 0
393 Pi ARDUINO multi function shield 핀정보 및 FND 소스 2016.11.21 7969 0
392 PHP Simple GD example 2014.07.10 8052 0
391 JAVA 자바 프로그래머가 알아야할 10가지 이클립스 단축키 1 2015.10.27 8083 0
390 Pi 라즈베리파이 적외선(IR) 리모컨 송신/수신 - LIRC Library 2017.10.17 8098 0
389 일반 NirCmd 사용법 2015.12.08 8153 0
388 JAVA 현재시간 구하기 2015.10.21 8230 0
387 일반 SQL Injection 우회 정리 2019.07.10 8353 0
목록
Board Pagination ‹ Prev 1 ... 3 4 5 6 7 8 9 10 11 12 ... 34 Next ›
/ 34

나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5