/// <summary>
/// 下载更新
/// </summary>
public string Update()
{
string result = "";
try
{
if (!isUpdate)
return result;
WebClient wc = new WebClient();
string filename = "Update_NewVersion.zip";
filename = Path.GetDirectoryName(loadFile) + "\\" + filename;
FinalZipName = filename;
//wc.DownloadFile(download, filename);//能下载成功
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileAsync(new Uri(download), filename);//使用DownloadFileAsync下载不成功,能正常运行代码,但是执行后没有下载到zip文件,
//wc.Dispose();
return result = "download:" + download + ",filename:" + filename;
}
catch (Exception ex)
{
throw new Exception("更新出现错误,网络连接失败!" + ex.Message);
}
}
void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
(sender as WebClient).Dispose();
if (e.Error != null)
throw e.Error;
else
isFinish();
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
CommonMethod.autostep = e.ProgressPercentage;
//Thread.Sleep(100);
}
The download file code using downloadfileasync works normally, but it is not downloaded to the file after execution. It has been tested that downloadfile can be downloaded to the file normally。
Is it because the program completed the running before the download finished? It is a possible reason if your app is a console application.
Following comes from mdsn:
https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfileasync?view=net-5.0
This method does not block the calling thread while the resource is being downloaded. To block while waiting for the download to complete, use one of the DownloadFile methods.
Related
I've created a little launcher program with my "Updater" code to download a file that has a version number. The updater tests that version number against a file that ships with the installer. If greater, download a .msi and execute. The problem is that on a Server 2012 machine the version file to be downloaded is empty upon arrival, while on a Server 2012 R2, the downloaded version is populated and correct. The same goes for my Dad's 8.1 machine. See code. Obviously, I don't own blah.com either.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Reflection;
namespace ra2kui
{
public partial class Updater : Form
{
Stopwatch sw = new Stopwatch();
string temppath = Path.GetTempPath();
public Updater()
{
InitializeComponent();
labelAssemblyVersion.Text = String.Format("Assembly Version: {0}", AssemblyVersion);
}
public string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
private void MainPage_Load(object sender, EventArgs e)
{
string currentver;
if (File.Exists(#"lib\\vo.r2k"))
{
using (StreamReader currentverreader = new StreamReader("lib\\vo.r2k"))
{
currentver = currentverreader.ReadLine() ?? "";
}
labelCurrentVersion.Text = "Current Package Version: " + currentver;
}
else
{
MessageBox.Show("VO missing.");
return;
}
buttonMulti.Text = "Downloading Version File From Server...";
labelVersionOnServer.Text = "Server Package Version: ";
if (File.Exists(temppath + #"2kuv.r2k"))
{
File.Delete(temppath + #"2kuv.r2k");
}
Application.DoEvents();
// Download the version from the server...
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
// Start the stopwatch
//Stopwatch sw = new Stopwatch();
sw.Start();
try
{
webClient.DownloadFileAsync(new Uri("https://www.blah.com/package/2kuv.r2k"), temppath + #"2kuv.r2k");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
// Progress bar value change
progressBar1.Value = e.ProgressPercentage;
// Calculate download speed and output it to labelSpeed.
labelSpeed.Text = string.Format("{0} kB/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0"));
// Show the percentage on our label.
labelPerc.Text = e.ProgressPercentage.ToString() + "%";
// Update the label with how much data have been downloaded so far and the total size of the file we are currently downloading
labelDownloaded.Text = string.Format("{0} MB's / {1} MB's",
(e.BytesReceived / 1024d / 1024d).ToString("0.00"),
(e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
string updatever;
string origver;
using (StreamReader updatereader = new StreamReader(temppath + "2kuv.r2k"))
{
updatever = updatereader.ReadLine() ?? "";
}
using (StreamReader origreader = new StreamReader("lib\\vo.r2k"))
{
origver = origreader.ReadLine() ?? "";
}
if (String.Compare(origver,updatever) > 0)
{
MessageBox.Show("There is no update available. Or there was an issue with the UV file.");
this.Close();
}
else if(String.Compare(origver,updatever) < 0)
{
MessageBox.Show("There is an update available.");
labelVersionOnServer.Text = "Server Package Version: " + updatever;
buttonMulti.Text = "Download and proceed with update...";
buttonMulti.Enabled = true;
}
else if(String.Compare(origver,updatever) == 0)
{
MessageBox.Show("You are on the most current version.");
this.Close();
}
}
private void buttonMulti_Click(object sender, EventArgs e)
{
buttonMulti.Text = "Downloading update...";
buttonMulti.Enabled = false;
Application.DoEvents();
//Create Stopwatch
sw.Start();
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed2);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
try
{
webClient.DownloadFileAsync(new Uri("https://www.blah.com/package/setup32.msi"), temppath + #"setup32.msi");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Completed2(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("The updater will now close and start the update.");
try
{
Process.Start(temppath + "setup32.msi");
Application.Exit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.Exit();
}
}
}
}
Before you flag this as a duplicate, yes there are questions just like this, i've looked at all of them and still couldn't get this working. I'm trying to code in a feature that downloads and runs a .exe file but it doesn't download, run or do anything. I even removed the try catches to find an error or error codes but I have non, so I have no idea where i'm going wrong, here is my code for it
public test_Configuration()
{
InitializeComponent();
}
Uri uri = new Uri("http://example.com/files/example.exe");
string filename = #"C:\Users\**\AppData\Local\Temp\example.exe";
private void button1_Click(object sender, EventArgs e)
{
try
{
if(File.Exists(filename))
{
File.Delete(filename);
}
else
{
WebClient wc = new WebClient();
wc.DownloadDataAsync(uri, filename);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = 0;
}
}
private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if(e.Error == null)
{
MessageBox.Show("Download complete!, running exe", "Completed!");
Process.Start(filename);
}
else
{
MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
}
Change DownloadDataAsync to DownloadFileAsync.
wc.DownloadFileAsync(uri, filename);
This code helped me out quite a bit with updating a file, so I thought I would show my twist in the hopes that someone else out there has a similar requirement as me.
I needed this code to do the following when a button was clicked:
Grab a file from a sever and store it locally in AppData\Temp.
Keep my user up-to-date of install progress (an installer is downloaded).
If successfully downloaded (note the removal of the else after deleting old file check), launch "daInstaller.exe", whilst terminating the current running program.
And if said file already exist (i.e. the old "daIstaller.exe"), delete prior to copying new file to AppData\Temp.
Don't forget to keep the file names the same, else you'll be leaving more trash in that AppData\Temp folder.
private void button1_Click(object sender, EventArgs e)
{
Uri uri = new Uri("http://example.com/files/example.exe");
filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Temp/example.exe");
try
{
if (File.Exists(filename))
{
File.Delete(filename);
}
WebClient wc = new WebClient();
wc.DownloadFileAsync(uri, filename);
wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = 0;
}
}
private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
Process.Start(filename);
Close();
Application.Exit();
}
else
{
MessageBox.Show("Unable to download exe, please check your connection", "Download failed!");
}
}
So basically my DownloadFile is:
public void DownloadFile()
{
settings_btn.Enabled = false;
label1.Text = "Checking for updates...";
//Defines the server's update directory
string Server = "http://downloadurl/update/";
//Defines application root
string Root = AppDomain.CurrentDomain.BaseDirectory;
//Make sure version file exists
FileStream fs = null;
if (!File.Exists("pversion"))
{
using (fs = File.Create("pversion")){}
using (StreamWriter sw = new StreamWriter("pversion")){sw.Write("1.0");}
}
//checks client version
string lclVersion;
using (StreamReader reader = new StreamReader("pversion"))
{
lclVersion = reader.ReadLine();
}
decimal localVersion = decimal.Parse(lclVersion);
//server's list of updates
XDocument serverXml = XDocument.Load(#Server + "pUpdates.xml");
//The Update Process
foreach (XElement update in serverXml.Descendants("pupdate"))
{
string version = update.Element("pversion").Value;
string file = update.Element("pfile").Value;
decimal serverVersion = decimal.Parse(version);
string sUrlToReadFileFrom = Server + file;
string sFilePathToWriteFileTo = Root + file;
if (serverVersion > localVersion)
{
using (webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
// The variable that will be holding the url address (making sure it starts with http://)
Uri url = new Uri(sUrlToReadFileFrom);
// Start the stopwatch which we will be using to calculate the download speed
sw.Start();
try
{
// Start downloading the file
webClient.DownloadFileAsync(url, sFilePathToWriteFileTo);
// Change the currently running executable so it can be overwritten.
Process thisprocess = Process.GetCurrentProcess();
string me = thisprocess.MainModule.FileName;
string bak = me + ".bak";
if (File.Exists(bak))
{
File.Delete(bak);
}
File.Move(me, bak);
File.Copy(bak, me);
//unzip
using (ZipFile zip = ZipFile.Read(file))
{
foreach (ZipEntry zipFiles in zip)
{
zipFiles.Extract(Root + "\\", true);
}
}
//download new version file
webClient.DownloadFile(Server + "pversion.txt", #Root + "pversion");
//Delete Zip File
deleteFile(file);
var spawn = Process.Start(me);
thisprocess.CloseMainWindow();
thisprocess.Close();
thisprocess.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
And my problem is that once it finds a new version and starts downloading the file webClient.DownloadFileAsync(url, sFilePathToWriteFileTo);, it instantly runs the code below which is the changing name, unzipping and downloading new version file progress
What I want it to do is wait for it to finish downloading the file, and THEN do the rest. How do I do this?
-- In case this is necessary, ProgressChanged:
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label3.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00")) + " " + string.Format("{0} MB's / {1} MB's", (e.BytesReceived / 1024d / 1024d).ToString("0.00"), (e.TotalBytesToReceive / 1024d / 1024d).ToString("0.00"));;
progressBar1.Value = e.ProgressPercentage;
label1.Text = e.ProgressPercentage.ToString() + "%";
}
and Completed:
private void Completed(object sender, AsyncCompletedEventArgs e)
{
sw.Reset();
if (e.Cancelled == true)
{
label1.Text = "Download cancelled!";
}
else
{
label1.Text = "Download completed!";
}
}
You can use the DownloadFile method. The Async word means that this method will run asynchronous (in other thread), that's why it's goes to next line praticaly instantly.
If you want wait for download ends, use the DownloadFile instead of DownloadFileAsync
How can I pause the loop in someRandomMethod() until the code in DownloadCompleted() have been executed? This code below only unpacks the latest version in the versions array. It's like the loop is faster than the first download and m_CurrentlyDownloading have the latest value the first time DownloadCompleted() is beeing executed.
private void someRandomMethod() {
for (int i = 0; i < versions.Count; i++)
{
//ClearInstallFolder();
m_CurrentlyDownloading = versions.ElementAt(i);
Download(versions.ElementAt(i));
LocalUpdate(versions.ElementAt(i));
System.Threading.Thread.Sleep(500);
}
}
private void Download(string p_Version)
{
string file = p_Version + ".zip";
string url = #"http://192.168.56.5/" + file;
//client is global in the class
client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
client.DownloadFileAsync(new Uri(url), #"C:\tmp\" + file);
}
private void DownloadCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
Unpack(m_CurrentlyDownloading);
if (GetInstalledVersion() == GetLatestVersion())
ClearZipFiles();
}
else
MessageBox.Show(e.Error.ToString());
}
The easiest way would be to not use the *async methods. The normal DownloadFile will pause execution until it completes.
But if you've got access to the Await keyword, try this.
private async Task Download(string p_Version)
{
string file = p_Version + ".zip";
string url = #"http://192.168.56.5/" + file;
//client is global in the class
client = new WebClient();
client.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressChanged);
await client.DownloadFileAsync(new Uri(url), #"C:\tmp\" + file);
}
something like this can be used to wait
make it class property
bool IsDownloadCompleted=false;
Add this in DownloadCompletedEvent
IsDownloadCompleted=true;
and this where you want to stop loop
while(DownloadCompleted!=true)
{
Application.DoEvents();
}
Create some boolean variable, create a delegate and get\set methods for this variable.
Then just in loop made smth like :
while(!isDownLoadCompleted)Thread.Sleep(1024);
You Can use Paralel.ForEach. this loop will wait until all threads done.
check Here for how to use :
http://msdn.microsoft.com/tr-tr/library/dd460720(v=vs.110).aspx
or
http://blogs.msdn.com/b/pfxteam/archive/2012/03/05/10278165.aspx
I've got a problem. How can I cancel a download ?
client.CancelAsync();
Doesn't work for me, because if I cancel a download and start a new one the code still tries to access the old download file. You've to know in my code is a part when a download is done it should unzip the file which has been downloaded. Example.zip like this :)
So, when I cancel my download and start a new one you know my script tries to unzip my old Example.zip, but it should kick this ....
For Unzipping, I'm using Iconic.Zip.dll (http://dotnetzip.codeplex.com/)
How to get it work?
UPDATE:
This is my Downloader Form
private void button3_Click_1(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("This will cancel your current download ! Continue ?", "Warning !", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
cancelDownload = true;
URageMainWindow.isDownloading = false;
this.Close();
}
else if (dialogResult == DialogResult.No)
{
}
}
This is my Main form this happens when you start downloading something
private void checkInstall(object sender, WebBrowserDocumentCompletedEventArgs e)
{
string input = storeBrowser.Url.ToString();
// Test these endings
string[] arr = new string[]
{"_install.html"};
// Loop through and test each string
foreach (string s in arr)
{
if (input.EndsWith(s) && isDownloading == false)
{
// MessageBox.Show("U.Rage is downloading your game");
Assembly asm = Assembly.GetCallingAssembly();
installID = storeBrowser.Document.GetElementById("installid").GetAttribute("value");
// MessageBox.Show("Name: " + installname + " ID " + installID);
installname = storeBrowser.Document.GetElementById("name").GetAttribute("value");
installurl = storeBrowser.Document.GetElementById("link").GetAttribute("value");
isDownloading = true;
string install_ID = installID;
string Install_Name = installname;
// MessageBox.Show("New Update available ! " + " - Latest version: " + updateversion + " - Your version: " + gameVersion);
string url = installurl;
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_InstallProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_InstallFileCompleted);
client.DownloadFileAsync(new Uri(url), #"C:\U.Rage\Downloads\" + installID + "Install.zip");
if (Downloader.cancelDownload == true)
{
//MessageBox.Show("Downloader has been cancelled");
client.CancelAsync();
Downloader.cancelDownload = false;
}
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(2, "Downloading !", "U.Rage is downloading " + installname, ToolTipIcon.Info);
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"c:/U.Rage/Sounds/notify.wav");
player.Play();
storeBrowser.GoBack();
igm = new Downloader();
igm.labelDWGame.Text = installname;
// this.Hide();
igm.Show();
return;
}
if (input.EndsWith(s) && isDownloading == true)
{
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"c:/U.Rage/Sounds/notify.wav");
player.Play();
MessageBox.Show("Please wait until your download has been finished", "Warning");
storeBrowser.GoBack();
}
}
}
This happens when the download has been finished
void client_InstallFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if(Downloader.cancelDownload == false)
{
using (ZipFile zip = ZipFile.Read(#"C:\U.Rage\Downloads\" + installID + "Install.zip"))
{
//zip.Password = "iliketrains123";
zip.ExtractAll("C:/U.Rage/Games/", ExtractExistingFileAction.OverwriteSilently);
}
System.IO.File.Delete(#"C:/U.Rage/Downloads/" + installID + "Install.zip");
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(2, "Download Completed !", "Installing was succesful !", ToolTipIcon.Info);
System.Media.SoundPlayer player = new System.Media.SoundPlayer(#"c:/U.Rage/Sounds/notify.wav");
player.Play();
this.Show();
igm.Close();
isDownloading = false;
listView.Items.Clear();
var files = Directory.GetFiles(#"C:\U.Rage\Games\", "*.ugi").Select(f => new ListViewItem(f)).ToArray();
foreach (ListViewItem f in files)
{
this.LoadDataFromXml(f);
}
}
}
Here is the method for async data download that supports cancellation:
private static async Task<byte[]> downloadDataAsync(Uri uri, CancellationToken cancellationToken)
{
if (String.IsNullOrWhiteSpace(uri.ToString()))
throw new ArgumentNullException(nameof(uri), "Uri can not be null or empty.");
if (!Uri.IsWellFormedUriString(uri.ToString(), UriKind.Absolute))
return null;
byte[] dataArr = null;
try
{
using (var webClient = new WebClient())
using (var registration = cancellationToken.Register(() => webClient.CancelAsync()))
{
dataArr = await webClient.DownloadDataTaskAsync(uri);
}
}
catch (WebException ex) when (ex.Status == WebExceptionStatus.RequestCanceled)
{
// ignore this exception
}
return dataArr;
}
When you call CancelAsync, the AsyncCompletedEventArgs object passed to the completed callback will have the Cancelled property set to true. So you could write:
void client_InstallFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if(e.Cancelled)
{
// delete the partially-downloaded file
return;
}
// unzip and do whatever...
using (ZipFile zip = ZipFile.Read(#"C:\U.Rage\Downloads\" + installID + "Install.zip"))
See the documentation for more info.
The selected answer didn't work properly for me. Here's what I did:
When they click the cancel button I call the
Client.CancelAsync();
And then in the Web.Client DownloadFileCompleted:
Client.DownloadFileCompleted += (s, e) =>
{
if (e.Cancelled)
{
//cleanup delete partial file
Client.Dispose();
return;
}
}
And then when you try to re-download just instantiate a new client:
Client = WebClient();
This way the old async parameters won't be maintained.