开始编写一个简单的控制台应用程序,将文件下载到当前位置。出于某种原因,无论文件有多大,我使用的WebClient只下载1kb的文件(无论多大)。我已经尝试在标题中添加浏览器,我尝试添加“while(WC.IsBusy)”(我在google搜索时发现的一个建议),我甚至尝试向已完成的处理程序添加错误处理程序以查看是什么继续,但抛出“对象引用未设置为对象的实例。”。我把头发拉到这里,我希望有人能看到我不喜欢的东西。
这是我的代码:
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() + "%");
}
}
}
答案 0 :(得分:0)
试试这个。仅供参考,您在此处使用并引用了Windows Forms特定的命名空间。
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() + "%");
}
}
}
答案 1 :(得分:0)
在下载终止之前异步退出程序。
WC.IsBusy
解决方案似乎是合法的,也许您应该尝试发布该代码。
然后我们将看到如何解决这个问题。
错误处理程序似乎是一个非常好的主意,你应该有一个以防万一。