当前位置: 首页>>代码示例>>C#>>正文


C# WebClient.DownloadFile方法代码示例

本文整理汇总了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);
            }
        }
    }
开发者ID:HansS,项目名称:TelerikAcademy-homework,代码行数:31,代码来源:FileDownloader.cs

示例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!!!");
        }
    }
开发者ID:purlantov,项目名称:telerik_academy,代码行数:26,代码来源:DownloadFile.cs

示例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);
            }
        }
    }
开发者ID:LafForce,项目名称:TelerikAcademyHWs,代码行数:35,代码来源:DlFile.cs

示例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);
                }

            }
    }
开发者ID:kicata,项目名称:CSharpPartTwoNew,代码行数:26,代码来源:04+DownloadFileFromWeb.cs

示例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;
            }
        }
    }
开发者ID:unbelt,项目名称:Telerik,代码行数:29,代码来源:04.DownloadFile.cs

示例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!");
            }
        }

    }
开发者ID:GeorgiNik,项目名称:TelerikAcademy,代码行数:29,代码来源:04.+Download+file.cs

示例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!");
            } 
        }
    }
开发者ID:Jarolim,项目名称:AllMyHomeworkForTelerikAcademy,代码行数:31,代码来源:DownloadFile.cs

示例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();
        }
    }
开发者ID:zvet80,项目名称:TelerikAcademyHomework,代码行数:26,代码来源:DownloadFile.cs

示例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);
        }
    }
开发者ID:tddold,项目名称:TelerikAcademyHomework,代码行数:30,代码来源:DownloadFile.cs

示例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.");
            }
        }
    }
开发者ID:TimeTo,项目名称:AnonimousProject,代码行数:32,代码来源:FileDownload.cs

示例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();
     }
 }
开发者ID:dirk-dagger-667,项目名称:telerik-c--part2-lectures,代码行数:31,代码来源:Program.cs

示例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
        }
    }
开发者ID:kris4o1993,项目名称:Telerik-Academy,代码行数:27,代码来源:Download.cs

示例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();
        }
    }
开发者ID:kiko81,项目名称:Teleric-Academy-Homeworks,代码行数:32,代码来源:DownloadFile.cs

示例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");
     }
 }
开发者ID:rogpeppe,项目名称:rethumb-examples,代码行数:7,代码来源:example-cs.cs

示例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.");
        }
    }
开发者ID:NikolovNikolay,项目名称:Telerik-Homeworks,代码行数:30,代码来源:DownloadAFile.cs


注:本文中的WebClient.DownloadFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。