简单的网页下载

时间:2014-09-20 09:14:05

标签: c# .net

以下程序没有给我所需的输出:

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
string path = System.Windows.Forms.Application.StartupPath;
// 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,fileName );
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" +path);

在移动到指定的位置ieApplication.StartupPath时,我发现' ms-banner.gif'文件,但没有数据显示。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

您忘记指定保存文件的完整路径。

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
string path = System.Windows.Forms.Application.StartupPath;
// 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.
// Full path for saving file
path = path + fileName;
myWebClient.DownloadFile(myStringWebResource, path);