2015-06-03 37 views
0

我試圖創建一個「文件更新程序」,當程序打開時,檢測是否存在一個名爲 「mods」的文件夾,相應的文件及其大小(以字節爲單位) 。其實它的作品!但我有一個標籤,告知正在下載的文件,它只顯示最後一條消息「Actualizacion finalizada.Ya puedes jugar!」而不是「Descargando」+ ArmorSts「,」Descargando「+ TreeCpt」和blablabla。 ,我如何通知標籤我現在正在下載哪個文件以及哪些文件被更新?如果有人需要用英文查看信息,請告訴我。c# - 檢查文件是否存在並下載

string ArmorSts = "[1.7.10]ArmorStatusHUD-client-1.28.jar"; 
string TreeCpt = "[1.7.10]Treecapitator-universal-2.0.4.jar"; 
string AdvSolar = "AdvancedSolarPanel-1.7.10-3.5.1.jar"; 

private void Form1_Load(object sender, EventArgs e) 
{ 
    string path = @"C:\Users\" + System.Environment.UserName + "\\AppData\\Roaming\\.minecraft\\mods\\"; 
    if (!Directory.Exists(path)) /* Si no existe, entonces... */ 
    { 
     MessageBox.Show("La carpeta .minecraft (" + path + ") no se encuentra. Por favor instale minecraft.", 
     "Error al encontrar el directorio", 
     MessageBoxButtons.OK, 
     MessageBoxIcon.Exclamation); 
     /* Busquemos el directorio entonces... */ 
     DialogResult result = folderBrowserDialog1.ShowDialog(); 
     if (result == DialogResult.OK) 
     { 
      string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath); 
     } 
     /* si cancelamos entonces cerramos el programa */ 
     else 
     { 
      Application.Exit(); 
     } 
    } 
    /* Si la carpeta existe, entonces... */ 
    else { 
     /* Si el mod esta, entonces... */ 
     FileInfo mod_1 = new FileInfo(path + ArmorSts); 
     /* Checkeamos si pesa igual */ 
     if (File.Exists(path + ArmorSts) && mod_1.Length == 27281) 
     { 
      label1.Text = ArmorSts + " - Actualizado"; 
      goto Siguiente_1; 
     } 
     else 
     { 
      /* Si el mod no esta o pesa diferente, entonces */ 
      label1.Text = "Descargando " + ArmorSts; 
      /* Descargar en %appdata%/.minecraft/mods */ 
      WebClient webClient = new WebClient(); 
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
      webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + ArmorSts), path + ArmorSts); 
      goto Siguiente_1; 
     } 
Siguiente_1 : 
     FileInfo mod_2 = new FileInfo(path + TreeCpt); 
     if (File.Exists(path + TreeCpt) && mod_2.Length == 94792) 
     { 
      label1.Text = TreeCpt + " - Actualizado"; 
      goto Siguiente_2; 
     } 
     else 
     { 
      label1.Text = "Descargando " + TreeCpt; 
      /* Descargar en %appdata%/.minecraft/mods */ 
      WebClient webClient = new WebClient(); 
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
      webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + TreeCpt), path + TreeCpt); 
      goto Siguiente_2; 
     } 
Siguiente_2: 

     FileInfo mod_3 = new FileInfo(path + AdvSolar); 
     if (File.Exists(path + AdvSolar) && mod_3.Length == 305645) 
     { 
      label1.Text = AdvSolar + " - Actualizado"; 
      goto Siguiente_3; 
     } 
     else 
     { 
      label1.Text = "Descargando " + AdvSolar; 
      /* Descargar en %appdata%/.minecraft/mods */ 
      WebClient webClient = new WebClient(); 
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
      webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + AdvSolar), path + AdvSolar); 
      goto Siguiente_3; 
     } 
/* 
     ... 
     ... and on and on and on ... 
     ... 
     */ 
Siguiente_23: 
     FileInfo mod_24 = new FileInfo(path + Witchery); 
     if (File.Exists(path + Witchery) && mod_24.Length == 7009956) 
     { 
      label1.Text = Witchery + " - Actualizado"; 
      Thread.Sleep(1000); 
      ActualizacionFinalizada(); 
     } 
     else 
     { 
      label1.Text = "Descargando " + Witchery; 
      /* Descargar en %appdata%/.minecraft/mods */ 
      WebClient webClient = new WebClient(); 
      webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
      webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
      webClient.DownloadFileAsync(new Uri("http://xipher.16mb.com/JallenCraft/Mods_Actualizados/" + Witchery), path + Witchery); 
      ActualizacionFinalizada(); 
     } 
} 


private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    colorProgressBar1.Value = e.ProgressPercentage; 
} 

private void Completed(object sender, AsyncCompletedEventArgs e) 
{ 
    Actualizando(); 
    label1.Text = "Actualizado"; 
} 

private void ActualizacionFinalizada() 
{ 
    Actualizando(); 
    label1.Text = "Actualizacion finalizada. Ya puedes jugar!"; 
} 

我學習C#獨立,我知道這是不正確的方式(重複很少修改相同的代碼),如果有,爲了簡化代碼或最佳的性能,我將非常感激一個最好的辦法!

+0

你需要清理代碼,比如去掉goto並且再使用一些語言,然後才能在SO上得到直接的答案。轉向代碼審查堆棧交換,然後回到您的具體問題。 – Arran

+0

你可以閱讀有關乾淨的代碼。關於如何編寫可維護的代碼,有幾個非常好的提示和最佳實踐。因爲它站在這裏,所以對於SO來說這不是一個好問題,因爲它不會最小化問題的代碼。 –

+0

有關於清潔代碼的http://ccd.ralfw.domainfactory-kunde.de/die-tugenden/德國網站。但大多數術語都是英語。注意:該網站已經過重新設計,似乎並非所有鏈接都是最終格式。入口點是http://clean-code-developer.de/ –

回答

1

這切入了WinForms的基本原理。有一個線程用於更新用戶界面。當您在控件上設置更改其外觀的屬性時,會在該線程上觸發Paint事件。直到您將程序控制權還給該線程,您的更改纔會顯示出來。

你想在這裏做什麼來解決你的問題是將大部分Form_Load代碼移動到BackgroundWorker控件。這個控件顯式地爲長時間運行的方法提供了一個地方,否則會阻塞用戶界面線程。

另外:goto?真?