파일을 현재 위치로 다운로드하는 간단한 콘솔 앱을 작성하기 시작했습니다. 웬일인지, 얼마나 큰 파일이라도, 내가 사용하고있는 WebClient는 단지 1KB의 파일만을 다운로드한다. 나는 헤더에 브라우저를 추가하려고 시도했다. "while (WC.IsBusy)"(검색 도중 찾은 제안)을 추가하려고 시도했지만 완성 된 핸들러에 오류 처리기를 추가하여 그런데 "객체 참조가 객체의 인스턴스로 설정되지 않았습니다."라는 메시지가 throw됩니다. 나는 여기서 머리카락을 꺼내고 누군가 내가 볼 수없는 것을 볼 수 있기를 바라고 있습니다.콘솔 앱이 파일을 다운로드하지 못했습니다?
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()에 도달했습니다. 앱이 오류가 발생할 때마다 닫히지 않도록 앱을 닫았습니다. –