2014-02-19 4 views
0

파일을 현재 위치로 다운로드하는 간단한 콘솔 앱을 작성하기 시작했습니다. 웬일인지, 얼마나 큰 파일이라도, 내가 사용하고있는 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() + "%"); 
     } 
    } 
} 
+0

'Console.ReadKey()'줄에서 어떻게됩니까? 당신/사용자가 키를 누른 다음 WebClient가 범위를 벗어 납니까? –

+0

WebClient 오류가 발생한 후 WC.DownloadFileAsync 아래의 Console.ReadKey()에 도달했습니다. 앱이 오류가 발생할 때마다 닫히지 않도록 앱을 닫았습니다. –

답변

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 ++ setup입니다. 샘플로 시도한 결과, 위의 예제처럼 성공적으로 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() + "%"); 
     } 
    } 
} 
+0

"도움이 되었다면 답을 표시해주세요." 대답의 일부가 아닙니다. – spender

+0

약 10 분 후에 코드를 테스트 할 것입니다. 나는 현재 도로에 있으며 코딩 랩탑은 집에있다. RDP에 감사드립니다. –

+0

동일한 문제, 다운로드 1kb, 다음 오류가 발생합니다 (찾을 수없는 것). –

0

다운로드를 종료하기 전에 프로그램을 비동기 적으로 종료합니다. WC.IsBusy 솔루션이 합법적 인 것 같습니다. 대신 해당 코드를 게시하고 게시해야합니다. 그런 다음이를 수정하는 방법을 살펴 보겠습니다.

오류 처리기는 매우 좋은 생각 인 것 같습니다.

+0

내 대답에 WC.IsBusy 코드를 추가했습니다. –

관련 문제