检查文件是否存在并下载
本文关键字:下载 存在 是否 文件 检查 | 更新日期: 2023-09-27 17:53:02
我正在尝试制作一个"文件更新程序",当程序打开时,检测是否存在一个名为"mods",对应的文件及其大小(以字节为单位)。实际上它是有效的!但我有一个标签,通知什么文件正在下载,它只显示最后的消息"Actualizacion finalizada。Ya puedes jugar!"而不是"Descargando + ArmorSts","Descargando + TreeCpt"等等。,我如何用标签通知我现在正在下载的文件和更新的文件?如果有人需要看英文留言,请告诉我。
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#,我知道这不是正确的方式(重复相同的代码,很少修改),如果有一个最好的方法来简化这段代码或最好的性能,我将非常感激!
这触及了WinForms的基本原则。有一个线程用于更新用户界面。当您在更改控件外观的控件上设置属性时,它会导致在该线程上触发Paint
事件。在将程序控制权返回给该线程之前,您的更改将不可见。
你想在这里做什么来解决你的问题是移动大部分的Form_Load代码到一个BackgroundWorker控件。此控件显式存在是为了为长时间运行的方法提供一个位置,否则这些方法将阻塞用户界面线程。
Also: goto
?真的吗?