As most things in .NET, it turned out to be relatively simple. You download the file, you move it on top of the running app, you restart…
So without further ado, here’s the code:
private void UpdateApplication(string applicationDownloadURL)
{
string downloadedFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), “myApp.exe”);
// download the exe
bool shouldContinue = true;
try
{
WebClient wc = new WebClient();
wc.DownloadFile(applicationDownloadURL, downloadedFilePath);
}
catch (Exception ex)
{
shouldContinue = false;
}
if (shouldContinue)
{
// get running app path
string appPath = Application.ExecutablePath;
// copy over running app
try
{
// create archive’s app path
string archiveAppPath = appPath + “.old”;
// delete any old archived apps, archive the current app
// and move the downloaded app in place
System.IO.File.Delete(archiveAppPath);
System.IO.File.Move(appPath, archiveAppPath);
System.IO.File.Move(downloadedFilePath, appPath);
}
catch (Exception ex)
{
shouldContinue = false;
}
// restart the app
if (shouldContinue)
{
Application.Restart();
}
}
}
Немає коментарів:
Дописати коментар