Results 1 to 4 of 4

Thread: C# to download a file in your disk

  1. #1
    Join Date
    Nov 2009
    Posts
    64

    C# to download a file in your disk

    Hello to all. I have a query about file download in C#. I want to know the code to download a file from the internet to my disk using C# programming. I finding this quite difficult to code as I am very new to programming concept. I have tried a few codes but all my codes have given some or the other errors every time. So please if any one can explain to me how to do this coding or can provide me with this code than I will be a lot thankful to that person.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: C# to download a file in your disk

    Code:
    string URL="http://www.yourdomainname.com/file.zip";
    string FilePath="C:\file.zip";
    System.Net.WebClient Client = new WebClient();
    Client.DownloadFile(URL,FilePath);
    The above four lines of code will help you to downlaod files from web to your computer. In the first line you have to specify the website address of the file .i.e URL. In the second line a string variable is used to store the file path where the file is to be downloaded. The third and fourth line will then download the file to your designated folder.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: C# to download a file in your disk

    The WebClient class and its DownloadFile method are used to facilitate the download of a web file to the user's machine. The DownloadFile method's first parameter contains the internet address of the web file the user intends to download. This address is also technically known as 'url'. The second parameter defines the path on the user's machine where the user intends to download the web file.
    Here is a code to download file Synchronously in C-Sharp where the main thread is blocked until the file download is completed.
    Code:
    using System.Net;
    WebClient webClient = new WebClient();
    webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");

  4. #4
    Join Date
    Apr 2008
    Posts
    1,948

    Re: C# to download a file in your disk

    I know a method which is termed as Asynchronous method to download a file from web in Csharp. This method is useful to not block the main thread while is file is being downloaded. With help of event handlers the programmer can also indicate the download file's progress and whether it has completed download or not. The Asynchronous method to download a file is as follows:


    Code:
    private void btnDownload_Click(object sender, EventArgs EA)
    {
      WebClient webClient = new WebClient();
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
      webClient.DownloadFileAsync(new Uri("http://siteeg.com/fileeg.txt"), @"D:\fileeg.txt");
    }
    
    private void ProgressChanged(object sender, DownloadProgressChangedEventArgs P)
    {
      progressBar.Value = P.ProgressPercentage;
    }
    
    private void Completed(object sender, AsyncCompletedEventArgs Ace)
    {
      MessageBox.Show("Finished downloading file");
    }

Similar Threads

  1. How To Download To Disk (D) & Transfer From Disk (C)?
    By Abdulwahab786 in forum Hardware Peripherals
    Replies: 1
    Last Post: 06-02-2012, 11:16 PM
  2. Replies: 4
    Last Post: 07-02-2011, 10:19 AM
  3. cannot delete file -- cannot read from source file or disk
    By Amitgujaran in forum Windows XP Support
    Replies: 3
    Last Post: 11-11-2010, 09:55 PM
  4. Replies: 2
    Last Post: 26-11-2008, 06:56 PM
  5. XP: Cannot delete file: Cannot read from the source file or disk.
    By iexplorer0726 in forum Operating Systems
    Replies: 0
    Last Post: 10-09-2007, 05:07 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,572,164.51749 seconds with 16 queries