以下是一个使用C#下载zip文件的示例代码,不使用WebClient类:
using System;
using System.IO;
using System.Net;
class Program
{
static void Main(string[] args)
{
string url = "http://example.com/file.zip";
string savePath = "C:\\path\\to\\save\\file.zip";
using (var client = new WebClient())
{
using (var stream = client.OpenRead(url))
{
using (var fileStream = File.Create(savePath))
{
stream.CopyTo(fileStream);
}
}
}
Console.WriteLine("Zip file downloaded successfully.");
}
}
这个示例中,我们使用WebClient
类打开一个指定URL的流,然后将其复制到本地文件流中,最后将文件保存到指定路径。
你需要将url
变量替换为要下载的zip文件的URL,savePath
变量替换为要保存zip文件的本地路径。
请注意,这个示例是同步下载,如果你需要异步下载,可以使用WebClient.DownloadFileAsync
方法。