从C#中的给定URL下载文件

时间:2014-03-29 06:35:21

标签: c# url download

我想创建一个功能,允许我的Web应用程序每小时后从给定的URL下载文件。

应该是这样的:

  1. 转到指定的网址,下载zip文件(例如 - http://www.abc.com/FileFolder/GetUrFile.aspx?username=abc&password=000&filename=abc.zip
  2. 然后按代码解压缩并提取该文件
  3. 将该文件放在特定文件夹中
  4. 每1或2小时后重复此过程
  5. 感谢你, Ram Vinay Kumar

1 个答案:

答案 0 :(得分:1)

您的解决方案分为3个步骤:

1。从URL下载文件
看看System.Net.WebClient

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


2。解压缩文件
在.Net 4.5中,您可以查看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);


或者如果你想要更强大的解压缩解绿色,可能会考虑SharpZipLib
第3。调度程序
你可以看看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
    }

合并这些并编写解决方案。

相关问题