
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to download a file from a URL in C#?
A file can be downloaded from a URL using web client. It is available in System.Net namespace.
The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI.
The web client can be said as an application or web browser (like Google Chrome, Internet Explorer, Opera, Firefox, Safari) which is installed in a computer and used to interact with Web servers upon user’s request. It is basically a consumer application which collects processed data from servers.
A Client and a Server are two parts of a connection, these are two distinct machines, web client requests information, and the web server is basically a PC that is designed to accept requests from remote computers and send on the information requested. Web server is responsible for storing the information in order to be viewed by the clients and is also usually a Web Host. A Web host allows connections to the server to view said stored info.
The WebClient class in C# uses the WebRequest class to provide access to resources. WebClient instances can access data with any WebRequest descendant registered
with the WebRequest.RegisterPrefix method. The DownloadFile is used to download a file.
WebClient Client = new WebClient (); client.DownloadFile("url","path");
Example
Say we want to download an image from the path "https://downloadfreeimages.jpg" and save it the computer local directory, below is the code.
using System; using System.Net; namespace DemoApplication{ public class Program{ public static void Main(){ string url = "https://downloadfreeimages.jpg"; string savePath = @"D:\Demo\FreeImages.jpg"; WebClient client = new WebClient(); client.DownloadFile(url, savePath); Console.ReadLine(); } } }
Output
The above example will download the image from the URL provided and saves it to the given path.
D:\Demo
- Related Articles
- How to download image from url in Android?
- How to download and save an image from a given URL in android?
- How to download a file using Node.js?
- How To Raise a "File Download" Dialog Box in Python?
- How to create a new image from a WBMP file or URL using imagecreatefromwbmp() function in PHP?
- How to create a new image from a WEBP file or URL using imagecreatefromwebp() function in PHP?
- How to raise a "File Download" Dialog Box in Python CGI Programming?
- How to download a video using a URL and save it in an photo album using Swift?
- How to force file download with PHP?
- How to create a new image from a PNG file or URL using the imagecreatefrompng() function in PHP?
- How can I download a file on a click event using selenium?
- How to download a Tarball from GitHub on Linux?
- Raise a "File Download" Dialog Box using Perl
- How to trigger a file download when clicking an HTML button or JavaScript?
- Python Program to extract email-id from URL text file
