本文整理汇总了C#中WebClient.DownloadFile方法的典型用法代码示例。如果您正苦于以下问题:C# WebClient.DownloadFile方法的具体用法?C# WebClient.DownloadFile怎么用?C# WebClient.DownloadFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebClient
的用法示例。
在下文中一共展示了WebClient.DownloadFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
public static void Main()
{
WebClient downloader = new WebClient();
using (downloader)
{
try
{
downloader.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"c:\Users\Teodor\Desktop\New folder\test.jpg");
}
catch (ArgumentException ae)
{
Console.WriteLine("{0} - {1}", ae.GetType(), ae.Message);
}
catch (WebException webEx)
{
Console.WriteLine("{0} - {1}", webEx.GetType(), webEx.Message);
Console.WriteLine("Destination not found!");
}
catch (NotSupportedException supportEx)
{
Console.WriteLine("{0} - {1}", supportEx.GetType(), supportEx.Message);
Console.WriteLine(supportEx.Message);
}
catch (Exception allExp)
{
Console.WriteLine("{0} - {1}", allExp.GetType(), allExp.Message);
}
}
}
示例2: Download
static void Download(string url, string downloadDir)
{
try
{
WebClient client = new WebClient();
using (client)
{
client.DownloadFile(url, downloadDir);
}
Console.WriteLine("The file was successfully downloaded!!!");
}
catch (ArgumentNullException)
{
Console.Error.WriteLine("Missing url or download directory!!!");
}
catch (WebException)
{
Console.Error.WriteLine("You have entered a wrong url or you have not set a name for the file!!!");
}
catch (NotSupportedException)
{
Console.Error.WriteLine("The application can not download the file!!!");
}
}
示例3: Main
static void Main()
{
Console.WriteLine(@"Problem 4. Download file
Write a program that downloads a file from Internet (e.g. Ninja image)
and stores it the current directory.
Find in Google how to download files in C#.
Be sure to catch all exceptions and to free any used resources in the
finally block.
============================================================================
Solution:");
string sourceResource = "http://blogs.telerik.com/images/default-source/miroslav-miroslav/super_ninja.png?sfvrsn=2";
string localFileName = Path.GetFileName(sourceResource);
using (WebClient myWebClient = new WebClient())
{
try
{
Console.WriteLine("Start downloading {0}", sourceResource);
myWebClient.DownloadFile(sourceResource, localFileName);
Console.WriteLine("Download succesfull.");
Console.WriteLine("You can see downloaded file in: local folder of this program\\bin\\Debug\\");
}
catch (WebException ex)
{
Console.Write(ex.Message);
if (ex.InnerException != null)
Console.WriteLine(" " + ex.InnerException.Message);
else
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Something going wrong. Details: " + ex.Message);
}
}
}
示例4: Main
static void Main()
{
WebClient webClient = new WebClient();
using (webClient)
{
try
{
webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"..\..\Logo-BASD.jpg");
//this row will be executed only if downwoad is successfull
Console.WriteLine("The file is downloaded");
}
catch (ArgumentNullException ex1)
{
Console.WriteLine("{0}",ex1.Message);
}
catch (System.Net.WebException ex2)
{
Console.WriteLine("{0}", ex2.Message);
}
catch (System.NotSupportedException ex3)
{
Console.WriteLine("{0}", ex3.Message);
}
}
}
示例5: Main
static void Main()
{
using (WebClient web = new WebClient())
{
try
{
// Download and save the file
web.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"D:\Logo-BASD.jpg");
}
catch (WebException)
{
Console.WriteLine("An error occurred while downloading data, or the file does not exist.");
}
catch (NotSupportedException)
{
Console.WriteLine("The method has been called simultaneously on multiple threads.");
}
Console.Write("[Done] The file is saved in 'D:\\'" + Environment.NewLine +
" Open file? [Y/N]: ");
// open file option
switch (Console.ReadLine().ToLower())
{
case "y": OpenFile(); break;
default: return;
}
}
}
示例6: Main
static void Main()
{
using (WebClient downloadClient = new WebClient())
{
try
{
Console.WriteLine("Downloading..");
downloadClient.DownloadFile("http://telerikacademy.com/Content/Images/news-img01.png", "news-img01.png");
Console.WriteLine("Image was downloaded successfully in 'bin' directory of the project!");
}
catch (ArgumentException ex)
{
Console.WriteLine(ex.Message);
}
catch (WebException ex)
{
Console.WriteLine(ex.Message);
}
catch (NotSupportedException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.WriteLine("Goodbye!");
}
}
}
示例7: Main
static void Main()
{
Console.Write("Enter URL address: ");
string path = Console.ReadLine();
Console.Write("Enter file name: ");
string name = Console.ReadLine();
using (WebClient webClient = new WebClient())
{
try
{
using (WebClient Client = new WebClient())
{
Client.DownloadFile(path, name);
}
}
catch (ArgumentException)
{
Console.WriteLine("The path is zero-length/ one or more invalid chars!");
}
catch (WebException)
{
Console.WriteLine("Invalid address/ filename-null reference(empty)/ error while downloading!");
}
catch (System.Security.SecurityException)
{
Console.WriteLine("You do not have the required permission to write to local file!");
}
}
}
示例8: Main
static void Main()
{
WebClient webClient = new WebClient();
try
{
webClient.DownloadFile(internetResource, resourceName);
Console.WriteLine("downloading to: {0}",Path.GetFullPath(resourceName));
}
catch (System.ArgumentNullException ex)
{
Console.WriteLine(ex.Message.ToString());
}
catch (System.Net.WebException ex)
{
Console.WriteLine(ex.Message.ToString());
}
catch (System.NotSupportedException ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
FreeResources();
}
}
示例9: Main
static void Main()
{
try
{
WebClient webCilend = new WebClient();
// feel free to change the adress
string url = "http://telerikacademy.com/Content/Images/news-img01.png";
string fileName = "TelerikNinja.png";
webCilend.DownloadFile(url, fileName);
Console.WriteLine("The picture is saved at {0}.", Directory.GetCurrentDirectory());
}
catch (UnauthorizedAccessException exception)
{
Console.WriteLine(exception.Message);
}
catch (NotSupportedException exception)
{
Console.WriteLine(exception.Message);
}
catch (WebException exception)
{
Console.WriteLine(exception.Message);
}
catch (ArgumentException exception)
{
Console.WriteLine(exception.Message);
}
}
示例10: Main
static void Main()
{
string link = "http://www.devbg.org/img/Logo-BASD.jpg";
string file = "Logo-BASD.jpg";
using (WebClient myClient = new WebClient())
{
try
{
myClient.DownloadFile(link, file);
Console.WriteLine(@"The file is downloaded in project subfolder ...\04.FileDownload\bin\debug");
}
catch (ArgumentException)
{
Console.WriteLine("The web-address name is either empty, or read as empty. Please provide a valid address.");
}
catch (WebException)
{
Console.WriteLine("The web-address was not found. ");
Console.WriteLine("Please make sure the address is correct.");
Console.WriteLine("Please check your network connection.");
}
catch (NotSupportedException)
{
Console.WriteLine("This operation is already performed by another method.");
}
catch (SecurityException)
{
Console.WriteLine("Security Issue. You do not have permission to access this file.");
}
}
}
示例11: Main
static void Main()
{
Console.WriteLine("This program downloads a file from internet\n" +
"by given URL from the user.");
Console.Write("Please enter URL address: ");
string URL = Console.ReadLine();
Console.Write("Please enter file name: ");
string file = Console.ReadLine();
string fullAddress = URL + "/" + file;
WebClient webClient = new WebClient();
try
{
webClient.DownloadFile(fullAddress, file);
}
catch (ArgumentNullException)
{
Console.WriteLine("The address can not be null");
}
catch (WebException)
{
Console.WriteLine("Invalid address or empty file.");
}
catch (NotSupportedException)
{
Console.WriteLine("This method does not support simultaneous downloads.");
}
finally
{
webClient.Dispose();
}
}
示例12: Main
static void Main()
{
using (WebClient Client = new WebClient())
{
try
{
Client.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "pornpic.jpeg");
}
catch(ArgumentException)
{
Console.WriteLine("Empty url entered.");
}
catch(System.Net.WebException)
{
Console.WriteLine("Not valid url or target file entered.");
}
catch(Exception)
{
Console.WriteLine("FATAL ERROR! RUNN BEFORE IT LAYS EGGS");
}
//no need of finally because we use "using" keyword :P
}
}
示例13: Main
static void Main(string[] args)
{
string url = "http://telerikacademy.com/Content/Images/news-img01.png";
string fileName = "TelerikNinja.png";
WebClient downloadPic = new WebClient();
try
{
downloadPic.DownloadFile(url, fileName);
Console.WriteLine("The picture is saved at {0}.", Directory.GetCurrentDirectory());
}
catch (UnauthorizedAccessException exception)
{
Console.WriteLine(exception.Message);
}
catch (NotSupportedException exception)
{
Console.WriteLine(exception.Message);
}
catch (WebException exception)
{
Console.WriteLine(exception.Message);
}
catch (ArgumentException exception)
{
Console.WriteLine(exception.Message);
}
finally
{
downloadPic.Dispose();
}
}
示例14: Main
static void Main()
{
using (WebClient client = new WebClient())
{
client.DownloadFile("http://api.rethumb.com/v1/width/100/http://factor45.org/images/beach.jpg", @"beach.thumb.jpg");
}
}
示例15: Main
static void Main()
{
try
{
string url = "http://www.devbg.org/img/";
string fileName = "Logo-BASD.jpg", myDownload = null;
WebClient downloader = new WebClient();
myDownload = url + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myDownload);
downloader.DownloadFile(myDownload, fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, url);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
}
catch (ArgumentException)
{
Console.WriteLine("There is a problem with the url or file name format entered");
}
catch (WebException)
{
Console.WriteLine("Combinig the url and filename is invalid or an error occured while downloading data.");
}
catch (SecurityException)
{
Console.WriteLine("Permission writing issues.");
}
}