Dzwebs.Net

撰写电脑技术杂文十余年

WebClient.DownloadFile方法下载服务器文件到本地

Admin | 2016-12-27 6:01:31 | 被阅次数 | 7058

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  通过WebClient.DownloadFile方法下载服务器文件到本地。

  功能说明:用指定的 URI 的资源下载到本地文件。

  命名空间:   System.Net

  程序集:  System(位于 System.dll)

  参数说明:

  address:Type: System.String

  从其下载数据 URI。

  fileName:Type: System.String

  若要接收的数据的本地文件的名称。

  异常
  Exception     Condition
  ArgumentNullException    

  address 参数为 null。
  WebException    

  通过组合构成的 URI BaseAddress 和 address 无效。

  - 或 -

  filename 是 null 或 Empty。

  - 或 -

  文件不存在。

  -或者-下载数据时出错。
  NotSupportedException    

  该方法已在多个线程同时调用。

  DownloadFile 将下载到本地文件数据的方法从 URI 中指定 address 参数。 下载资源时此方法阻止。 若要下载资源,并在等待服务器响应的同时继续执行,使用一个 DownloadFileAsync 方法。

  如果 BaseAddress 属性不是空字符串 ("") 和 address 不包含绝对 URI, address 必须是与组合的相对 URI BaseAddress 构成请求的数据的绝对 URI。 如果 QueryString 属性不为空字符串,则会附加到 address。

  此方法使用 RETR 命令下载 FTP 资源。 对于 HTTP 资源时,使用 GET 方法。

  代码如下:

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


该杂文来自: 网站开发杂文

上一篇:利用WebClient抓取网页数据

下一篇:WebClient类的属性、方法与事件

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计