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"
但是命令提示符窗口中不显示下载进度或下载完成消息。我错在哪里?