本文整理汇总了C#中WebClient.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# WebClient.Dispose方法的具体用法?C# WebClient.Dispose怎么用?C# WebClient.Dispose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WebClient
的用法示例。
在下文中一共展示了WebClient.Dispose方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
WebClient webClient = new WebClient();
Console.WriteLine("Enter file url: ");
string downloadSource = Console.ReadLine();
Console.WriteLine("Enter destination on local hard drive. Specify file name and extension: ");
string downloadDestination = Console.ReadLine();
try
{
webClient.DownloadFile(downloadSource, downloadDestination);
}
catch (System.Net.WebException)
{
Console.WriteLine("Error accessing the network.");
}
catch (System.NotSupportedException)
{
Console.WriteLine("Method not supported or failed attempt to read, seek or write to a stream.");
}
catch (ArgumentNullException)
{
Console.WriteLine("A null reference cannot be accepted by this method!");
}
catch (ArgumentException)
{
Console.WriteLine("The path you entered is invalid.");
}
finally
{
webClient.Dispose();
}
}
示例2: Main
static void Main()
{
WebClient webClient = new WebClient();
string linkToFile = @"http://www.devbg.org/img/Logo-BASD.jpg";
try
{
webClient.DownloadFile( linkToFile,"logo.jpg");
}
catch (ArgumentNullException)
{
Console.WriteLine("You can't use null arguments");
}
catch (ArgumentException)
{
Console.WriteLine("'{0}' is a wrong path", linkToFile);
}
catch (NotSupportedException)
{
Console.WriteLine("Use different slashes for this operating system");
}
catch (WebException)
{
Console.WriteLine("Not file specified to save to");
}
finally
{
webClient.Dispose();
}
}
示例3: Main
static void Main()
{
WebClient webClient = new WebClient();
using (webClient)
{
try
{
Console.WriteLine("Downloading file...");
webClient.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", @"d:\myfile.jpg");
Console.WriteLine("Done.");
}
catch (ArgumentNullException an)
{
Console.WriteLine("ArgumentNullException: " + an.Message);
}
catch (ArgumentException ae)
{
Console.WriteLine("ArgumentException: " + ae.Message);
}
catch (WebException we)
{
Console.WriteLine("WebException: " + we.Message);
}
catch (NotSupportedException ns)
{
Console.WriteLine("NotSupportedException: " + ns.Message);
}
finally
{
webClient.Dispose();
GC.Collect();
}
}
}
示例4: Main
static void Main()
{
string decorationLine = new string('-', 80);
Console.Write(decorationLine);
Console.WriteLine("***Downloading a file from Internet and storing it in the current directory***");
Console.Write(decorationLine);
WebClient webClient = new WebClient();
try
{
string remoteUri = "http://www.telerik.com/images/newsletters/academy/assets/";
string fileName = "ninja-top.gif";
string fullFilePathOnWeb = remoteUri + fileName;
webClient.DownloadFile(fullFilePathOnWeb, fileName);
Console.WriteLine("Downloading '{0}' from {1} is completed!", fileName, remoteUri);
Console.WriteLine("You can find it in {0}.", Environment.CurrentDirectory);
}
catch (ArgumentNullException)
{
Console.WriteLine("No file address is given!");
}
catch (WebException webException)
{
Console.WriteLine(webException.Message);
}
catch (NotSupportedException)
{
Console.WriteLine("Downloading a file is in process! Cannot download multiple \nfiles simultaneously!");
}
finally
{
webClient.Dispose();
}
}
示例5: Main
static void Main()
{
using (WebClient webCl = new WebClient())
{
try
{
Console.WriteLine("Write a program that downloads a file from Internet (e.g. http://www.devbg.org/img/Logo-BASD.jpg) 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.");
Console.WriteLine();
webCl.DownloadFile("http://www.devbg.org/img/Logo-BASD.jpg", "image.jpg");
Console.WriteLine("Download Complete.");
}
catch (System.Net.WebException)
{
Console.WriteLine("Error downloading file.");
}
catch (System.ArgumentException)
{
Console.WriteLine("Wrong path");
}
finally
{
webCl.Dispose();
}
}
}
示例6: Main
static void Main()
{
Console.WriteLine("Enter the URL of the which you want to download:\nExample: \"http://www.d3bg.org/img/upload/tamplier/01.jpg\" ");
Uri uri = new Uri(Console.ReadLine());
string fileName = System.IO.Path.GetFileName(uri.LocalPath);
WebClient webClient = new WebClient();
try
{
// "../../" changes the default location of the downloaded file to two directories above!
webClient.DownloadFile(uri, "../../" + fileName);
Console.WriteLine("The file was successfully downloaded!\n!");
}
catch (WebException)
{
Console.WriteLine("Invalid adress or empty file.");
}
catch (NotSupportedException)
{
Console.WriteLine("");
}
finally
{
webClient.Dispose();
}
}
示例7: Main
static void Main()
{
string remoteUri = "http://telerikacademy.com/Content/Images/";
string fileName = "news-img01.png";
string webResource = null;
WebClient myWebClient = new WebClient();
webResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n", fileName, webResource);
try
{
myWebClient.DownloadFile(webResource, fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, webResource);
}
catch (ArgumentNullException)
{
Console.WriteLine("No address given.");
}
catch (WebException)
{
Console.WriteLine("The file does not exist or the name is empty.");
}
catch (NotSupportedException)
{
Console.WriteLine("The method has been called simultaneously on multiple threads.");
}
finally
{
myWebClient.Dispose();
}
Console.WriteLine();
}
示例8: Main
static void Main()
{
string fileName="news-img01.png";
string path = GetSavePath(fileName);
string address = "http://telerikacademy.com/Content/Images/news-img01.png";
WebClient client = new WebClient();
try
{
client.DownloadFile(address, path);
Console.WriteLine("File saved in {0}", path);
}
catch (ArgumentNullException)
{
Console.WriteLine("The address can not be null");
}
catch (WebException)
{
Console.WriteLine("Invalid address or file name.");
}
catch (NotSupportedException)
{
Console.WriteLine("Simultaneous downloads are not supported.");
}
finally
{
client.Dispose();
}
}
示例9: ReadAndDownloadFile
private static void ReadAndDownloadFile()
{
WebClient wc = new WebClient();
try
{
string link = @"http://www.devbg.org/img/Logo-BASD.jpg";
wc.DownloadFile(link, "../../pic.jpg");
}
catch (ArgumentNullException ane)
{
Console.WriteLine(ane.Message);
}
catch (WebException we)
{
Console.WriteLine(we.Message);
}
catch (NotSupportedException nse)
{
Console.WriteLine(nse.Message);
}
finally
{
wc.Dispose();
}
}
示例10: Main
static void Main()
{
Console.Write("Please enter the path: ");
string path = Console.ReadLine();
string fileName = "ninja.gif";
WebClient myWebClient = new WebClient();
try
{
myWebClient.DownloadFile(path, fileName);
Console.WriteLine(@"The file is downloaded in Project\bin\Debug!");
}
catch (WebException)
{
Console.WriteLine("The adress is invalid.");
}
catch (ArgumentNullException)
{
Console.WriteLine("The address parameter is null.");
}
catch (NotSupportedException)
{
Console.WriteLine("The method has been called simultaneously on multiple threads.");
}
finally
{
myWebClient.Dispose();
}
}
示例11: Main
static void Main()
{
Console.WriteLine("Task 04 - Download a file from given URI\n\n");
string uri = "http://www.devbg.org/img/Logo-BASD.jpg";
Console.WriteLine();
Console.Write("Please enter full URI to download (or press enter for default): ");
string file = Console.ReadLine();
if (file.Length == 0) file = uri;
WebClient client = null;
try
{
string filename = Path.GetFullPath(".") + @"\" + Path.GetFileName(file);
Console.WriteLine("\nDownloading {0}\n\nDestination: {1}\n\n", file, filename);
client = new WebClient();
client.DownloadFile(file, filename);
}
catch (SystemException se)
{
Console.WriteLine("Exception: " + se.Message);
}
finally
{
if (client != null) client.Dispose();
}
Console.WriteLine("\n\nPress Enter to finish");
Console.ReadLine();
}
示例12: Main
static void Main()
{
WebClient client = new WebClient();
try
{
client.DownloadFile("http://telerikacademy.com/Content/Images/news-img01.png", "ninja.png");
}
catch (UriFormatException e)
{
Console.WriteLine(e.Message);
}
catch (HttpListenerException e)
{
Console.WriteLine(e.Message);
}
catch (WebException e)
{
Console.WriteLine(e.Message);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
}
catch (NotSupportedException e)
{
Console.WriteLine(e.Message);
}
finally
{
client.Dispose();
}
}
示例13: Main
static void Main(string[] args)
{
WebClient downloader = new WebClient();
string uri = "http://www.devbg.org/img/Logo-BASD.jpg";
string destination = Directory.GetCurrentDirectory() + @"\Logo-BASD.jpg";
try
{
downloader.DownloadFile(uri, destination);
}
catch (WebException we)
{
Console.WriteLine(we.Message);
}
catch (NotSupportedException nse)
{
Console.WriteLine(nse.Message); ;
}
catch (ArgumentException ae)
{
Console.WriteLine(ae.Message);
}
finally
{
downloader.Dispose();
}
}
示例14: GetTimeFromDDG
private DateTime GetTimeFromDDG()
{
//https://duckduckgo.com/?q=what+time+is+it
WebClient wc = new WebClient();
try
{
char[] separators = new char[] { '<', '>', '\\', '/', '|' };
string timeisPage = wc.DownloadString("https://duckduckgo.com/html/?q=what%20time%20is%20it");
timeisPage = timeisPage.Remove(0, timeisPage.IndexOf("\n\n\t\n\n\n\n ") + 19);
string[] timeisSplit = timeisPage.Split(separators, StringSplitOptions.RemoveEmptyEntries);
string Time = timeisSplit[0].Remove(timeisSplit[0].Length - 5);
DateTime result;
if (DateTime.TryParse(Time, out result))
{
return result;//.ToString("t");
}
throw new Exception();
}
catch
{
return DateTime.Now;//.ToString("t");
}
finally
{
wc.Dispose();
GC.Collect();
}
}
示例15: Main
static void Main()
{
WebClient client = new WebClient();
try
{
string adressAsString = "http://www.devbg.org/img/Logo-BASD.jpg";
string location = "Image.jpg";
client.DownloadFile(adressAsString, location);
Console.WriteLine("The download is complete. You can find your file in bin/Debug");
}
catch (ArgumentNullException)
{
Console.WriteLine("The adress parameter is with null value");
}
catch (WebException)
{
Console.WriteLine("Invalid adress, the file name is empty or null, or the file does not exist");
}
catch (NotSupportedException)
{
Console.WriteLine("The method has been called simultaneously on multiple targets");
}
finally
{
client.Dispose();
}
}