根据Microsoft 文档中的示例,该文件将下载到Application.StartupPath
. 如果只提供了一个文件名,它将保存在与您的可执行文件相同的路径中。
请记住,对于某些应用程序(例如 ASP.NET Web 应用程序),将文件保存在与可执行文件相同的文件夹中可能存在权限问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string remoteUri = "put your url here";
string fileName = "image.php_.jpeg", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, @"c:\tmp\" + fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
}
}
}