c# - 检查文件是否存在并下载

时间:2015-06-03 21:43:51

标签: c#

我试图创建一个"文件更新程序"当程序打开时,检测是否存在名为的文件夹 " mods",相应的文件及其大小(以字节为单位)。实际上它有效!但我有一个标签,告知正在下载的文件,它只显示最后一条消息" Actualizacion finalizada.Ya puedes jugar!"而不是" Descargando" + ArmorSts"," Descargando" + TreeCpt"和blablabla。 ,我如何通过标签告知我现在正在下载什么文件以及更新了哪些文件?如果有人需要用英语看消息,请告诉我。

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#,我知道这不是正确的方法(重复相同的代码,只需稍加修改),如果有最好的方法来简化此代码或以最佳性能,我将非常感激!

1 个答案:

答案 0 :(得分:1)

这削弱了WinForms的基本原则。有一个用于更新用户界面的线程。在更改其外观的控件上设置属性时,会导致在该线程上触发Paint事件。在将程序控制返回到该线程之前,您的更改将不可见。

您要在此处解决问题的方法是将大部分Form_Load代码移至BackgroundWorker控件。此控件显式存在,为长时间运行的方法提供了一个位置,否则会阻塞用户界面线程。

另外:goto?真?