I have the main app and an updater. I start the updater from the main program and right after the updater started, i kill the main program. All ok by now. Now, i check a xml file from a website for files that must be updated. I use this to download the files:
Stopwatch sw = new Stopwatch();
public void DownloadFile(string urlAddress, string location)
{
using (WebClient webClient = new WebClient())
{
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
Uri URL = urlAddress.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ? new Uri(urlAddress) : new Uri("http://" + urlAddress);
sw.Start();
try
{
webClient.DownloadFileAsync(URL, location);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
labelSpeed.Text = string.Format("{0} kb/s", (e.BytesReceived / 1024d / sw.Elapsed.TotalSeconds).ToString("0.00"));
progressBar.Value = e.ProgressPercentage;
labelPerc.Text = e.ProgressPercentage.ToString() + "%";
downloaded.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)
{
sw.Reset();
if (e.Error != null)
{
MessageBox.Show(e.Error.InnerException.Message,e.Error.Message);
}
if (e.Cancelled == true)
{
MessageBox.Show("Download has been canceled.");
}
else
{
MessageBox.Show("Download completed!");
}
}
And i call the download method using:
foreach (string file in dld)
{
DownloadFile(file, AppDomain.CurrentDomain.BaseDirectory+file);
MessageBox.Show("done with:" + file);
}
where, dld is List<string> dld = new List<string>();
When i call the download method, i get this error:
"An exception occured during a WebClient request.
The process cannot access the file xxx because it is used by another process."
PS: The updater is launched with admin rights.
I really do not understand what is the problem with the code. I've already asked here for the same problem, but it was only for one file(solved it with admin rights). Now i have multiple files to download and it doesn't work , even if the program is started with admin rights. I double checked, and the main program is closed.
Appreciate any help.
LATER EDIT : I think i found the problem. I have somewhere
Assembly assembly = Assembly.LoadFrom(path);
Version ver = assembly.GetName().Version;
and i think this command locks the file.
Now i have to find out how to load and unload assembly. Back to google.
LAST EDIT : Found the solution and it works.
AssemblyName assembly = AssemblyName.GetAssemblyName(path);
Version ver = assembly.Version;