ShubhVinay
ShubhVinay

Reputation: 13

Download File from given URL in C#

I want to create a functionality that allow my web application to download the file from given URL after every hour.

Should be something like this :

  1. It goes to given URL, download the zip file (eg - http://www.abc.com/FileFolder/GetUrFile.aspx?username=abc&password=000&filename=abc.zip)
  2. Then Unzip and extract that file by code
  3. Put that file in specific folder
  4. Repeat this process after every 1 or 2 hours

Thanking You, Ram Vinay Kumar

Upvotes: 0

Views: 5498

Answers (1)

Loki
Loki

Reputation: 276

Your solution would be in 3 steps:

1. Download file from URL
Have a look at System.Net.WebClient

using (WebClient Client = new WebClient ())
{
    Client.DownloadFile("http://www.abc.com/file/song/a.mpeg", "a.mpeg");
}


2. Unzip the file
In .Net 4.5, you can look at System.IO.Compression.ZipFile

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";
string extractPath = @"c:\example\extract";

ZipFile.CreateFromDirectory(startPath, zipPath);
ZipFile.ExtractToDirectory(zipPath, extractPath);


Or probably look into SharpZipLib if you want more robust solution to unzip

3. Scheduler
you can look at System.Timers;

    public void MyMethod()
    {
        Timer timer = new Timer();
        timer.Interval = new TimeSpan(0, 15, 0).TotalMilliseconds;
        timer.AutoReset = true;
        timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
        timer.Enabled = true;
    }

    public void Timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        //Do your stuff here
    }

Combine these and code your solution.

Upvotes: 3

Related Questions