【问题标题】:Console App failing to download file?控制台应用程序无法下载文件?
【发布时间】:2014-02-19 17:20:08
【问题描述】:

开始编写一个简单的控制台应用程序,将文件下载到当前位置。出于某种原因,无论文件有多大,我使用的 WebClient 只下载 1kb 的文件(无论有多大)。我尝试将浏览器添加到标题中,我尝试添加“while (WC.IsBusy)”(我在谷歌搜索时发现的一个建议),我什至尝试将错误处理程序添加到已完成的处理程序以查看是什么继续,但这会引发“对象引用未设置为对象的实例。”。我正在把头发拉出来,我希望有人能看到我看不到的东西。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Windows.Forms;

namespace csgocfgmakerupdater
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                instalwithargs(args);
            }
            else
            {
                installnoargs();
            }
        }

        static void installnoargs()
        {
            Console.Clear();
            Console.WriteLine("Current Folder: " + Directory.GetCurrentDirectory());
            Console.WriteLine("");
            Console.WriteLine("This will install the Counter-Strike: Global Offensive Config File Maker to your computer. Please select an option from the following:");
            Console.WriteLine("");
            Console.WriteLine("1. Install to Current Folder");
            Console.WriteLine("2. Install to Custom Folder");
            Console.WriteLine("3. Update Existing Installation in Current Folder");
            Console.WriteLine("4. Update Existing Installation in Custom Folder");
            Console.WriteLine("5. Exit");
            string installcmd = Console.ReadLine();
            switch (installcmd)
            {
                case "1":
                    try
                    {
                        string updurl = "http://cachefly.cachefly.net/100mb.test";//"http://elite.so/projects/cs-go-game-config-editor/CSGOCFGMKR.exe";
                        WebClient WC = new WebClient();
                        WC.DownloadProgressChanged += new DownloadProgressChangedEventHandler(WC_DownloadProgressChanged);
                        WC.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(WC_DownloadFileCompleted);
                        WC.DownloadFileAsync(new Uri(updurl), "100mb.test");
//this waits for the webclient to exit 
while (WC.IsBusy) {
Console.WriteLine("Downloading Updated files...");
}
                        Console.ReadKey();
                    }
                    catch (Exception ex)
                    {
                        Console.Clear();
                        Console.WriteLine(ex.Message);
                    }
                    break;
            }
        }
        static void WC_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.UserState != e.Error)
            {
                Console.Clear();
                Console.WriteLine("Update applied successfully!");
                Console.ReadKey();
                Environment.Exit(0);
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }

        static void WC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {  
            Console.Clear();
            Console.WriteLine("Downloading Updated files...");
            Console.WriteLine(progperc.ToString() + "%");
        }
    }
}

【问题讨论】:

  • Console.ReadKey() 行会发生什么?您/用户是否按下一个键然后 WebClient 超出范围?
  • Webclient 错误后到达 WC.DownloadFileAsync 下的 Console.ReadKey()。它被放在那里,这样应用程序就不会在每次出错时关闭。

标签: c# console-application webclient download


【解决方案1】:

您在下载终止之前异步退出程序。 WC.IsBusy 解决方案似乎合法,也许您应该尝试发布该代码。 然后我们会看看如何解决这个问题。

错误处理程序似乎是个好主意,你应该有一个以防万一。

【讨论】:

    【解决方案2】:

    试试这个。仅供参考,您在此处使用并引用了 Windows 窗体特定的命名空间。

    using System;
    using System.IO;
    using System.Net;
    
    namespace csgocfgmakerupdater
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length > 0)
                {
                 //   instalwithargs(args);
                }
                else
                {
                    installnoargs();
                }
            }
    
            static void installnoargs()
            {
                Console.Clear();
                Console.WriteLine("Current Folder: " + Directory.GetCurrentDirectory());
                Console.WriteLine("");
                Console.WriteLine("This will install the Counter-Strike: Global Offensive Config File Maker to your computer. Please select an option from the following:");
                Console.WriteLine("");
                Console.WriteLine("1. Install to Current Folder");
                Console.WriteLine("2. Install to Custom Folder");
                Console.WriteLine("3. Update Existing Installation in Current Folder");
                Console.WriteLine("4. Update Existing Installation in Custom Folder");
                Console.WriteLine("5. Exit");
                string installcmd = Console.ReadLine();
                switch (installcmd)
                {
                    case "1":
                        try
                        {
                            string updurl = "http://elite.so/projects/cs-go-game-config-editor/CSGOCFGMKR.exe";
                            WebClient WC = new WebClient();
                            WC.DownloadProgressChanged += new DownloadProgressChangedEventHandler(WC_DownloadProgressChanged);
                            WC.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(WC_DownloadFileCompleted);
                            WC.DownloadFileAsync(new Uri(updurl), "100mb.test");
                            Console.ReadKey();
                        }
                        catch (Exception ex)
                        {
                            Console.Clear();
                            Console.WriteLine(ex.Message);
                        }
                        break;
                }
            }
            static void WC_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
            {
                if (e.UserState != e.Error)
                {
                    Console.Clear();
                    Console.WriteLine("Update applied successfully!");
                    Console.ReadKey();
                    Environment.Exit(0);
                }
                else
                {
                }
            }
    
            static void WC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                Console.Clear();
                Console.WriteLine("Downloading Updated files...");
                Console.WriteLine(e.ProgressPercentage.ToString() + "%");
            }
        }
    }
    

    *回复您的评论: 下面是我修改文件 URL 以检查文件是否正确发送的相同代码。这是我作为示例尝试的 7 MB 以上的 Notepad++ 设置,它成功地将文件下载到我的 bin 文件夹中,就像前面的示例一样。

    using System;
    using System.IO;
    using System.Net;
    
    namespace csgocfgmakerupdater
    {
        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length > 0)
                {
                    //   instalwithargs(args);
                }
                else
                {
                    installnoargs();
                }
            }
    
            static void installnoargs()
            {
                Console.Clear();
                Console.WriteLine("Current Folder: " + Directory.GetCurrentDirectory());
                Console.WriteLine("");
                Console.WriteLine("This will install the Counter-Strike: Global Offensive Config File Maker to your computer. Please select an option from the following:");
                Console.WriteLine("");
                Console.WriteLine("1. Install to Current Folder");
                Console.WriteLine("2. Install to Custom Folder");
                Console.WriteLine("3. Update Existing Installation in Current Folder");
                Console.WriteLine("4. Update Existing Installation in Custom Folder");
                Console.WriteLine("5. Exit");
                string installcmd = Console.ReadLine();
                switch (installcmd)
                {
                    case "1":
                        try
                        {
                            //string updurl = "http://elite.so/projects/cs-go-game-config-editor/CSGOCFGMKR.exe";
                            string updurl = "http://download.tuxfamily.org/notepadplus/6.5.4/npp.6.5.4.Installer.exe";
                            WebClient WC = new WebClient();
                            WC.DownloadProgressChanged += new DownloadProgressChangedEventHandler(WC_DownloadProgressChanged);
                            WC.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(WC_DownloadFileCompleted);
                            WC.DownloadFileAsync(new Uri(updurl),"test.exe");
                            Console.ReadKey();
                        }
                        catch (Exception ex)
                        {
                            Console.Clear();
                            Console.WriteLine(ex.Message);
                        }
                        break;
                }
            }
            static void WC_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
            {
                if (e.UserState != e.Error)
                {
                    Console.Clear();
                    Console.WriteLine("Update applied successfully!");
                    Console.ReadKey();
                    Environment.Exit(0);
                }
                else
                {
                }
            }
    
            static void WC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
            {
                Console.Clear();
                Console.WriteLine("Downloading Updated files...");
                Console.WriteLine(e.ProgressPercentage.ToString() + "%");
            }
        }
    }
    

    【讨论】:

    • "如果对您有帮助,请标记为答案。"不是答案的一部分。
    • 我将在大约 10 分钟后测试代码。我现在在路上,我的编码笔记本电脑在家里。感谢上帝的 RDP!
    • 同样的问题,下载 1kb,然后遇到错误(我似乎找不到)。
    • 我在这里尝试了同样的方法,它给了我一个 51.5 KB 大小的文件。
    • 好的,等我回到我的笔记本电脑上,我会做一个新项目,然后再试一次。
    猜你喜欢
    • 2020-07-07
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2012-05-24
    • 2012-12-03
    • 2013-07-09
    • 2020-11-19
    相关资源
    最近更新 更多