0
using System.Windows.Forms;
using System.Net;
using System;
using System.ComponentModel;

namespace FileDownloadUIClient
{
    public partial class Form1 : Form
    {
        string[] arguments;
        public Form1(string[] args)
        {
            InitializeComponent();
            arguments = args;                
            download();
        }
        public void download()
        {
            if (arguments.Length < 0) { this.Close(); }
            else
            {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);                    
                client.DownloadFileAsync(new Uri(arguments[0]), DateTime.Now.Ticks.ToString() +".bin");
            }
        }        
        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            double bytesIn = double.Parse(e.BytesReceived.ToString());
            double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
            double percentage = bytesIn / totalBytes * 100;
            Console.WriteLine( int.Parse(Math.Truncate(percentage).ToString()));
        }
        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            Console.WriteLine("Download Completed");
            this.Close();
        }

主要方法:

using System.Windows.Forms;

namespace FileDownloadUIClient
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(args));
        }
    }
}

此(Windows 窗体应用程序)C# 程序在将文件下载 url作为参数传递时启动。

Ex:
C:\FileDownloadUIClient\bin\Debug>FileDownloadUIClient.exe "http://localhost/myfile.mp4"

但是命令提示符窗口中不显示下载进度或下载完成消息。我错在哪里?

4

4 回答 4

2

Windows 窗体应用程序没有控制台。

如果要查看控制台输出,请将项目类型更改为控制台应用程序。

于 2013-04-19T14:55:59.240 回答
1

如果您需要在 Windows 窗体上显示进度,请考虑使用该ProgressBar控件。Console正如其他人已经指出的那样,您不能使用。

于 2013-04-19T15:02:28.217 回答
1

Winform 默认不为您提供控制台,如果您只想使用“控制台”进行测试/监控,请尝试:

Debug.WriteLine(...);

您将在 VisualStudio 输出窗口中看到结果。

于 2013-04-19T15:02:38.613 回答
1

如果您的项目是 Targeted as,Windows Application Form则将其更改Console Application为您需要控制台以显示进度。


在此处输入图像描述

确保您有输出类型Console Application,之后它必须正确显示进度!

于 2013-04-19T14:59:09.503 回答