106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using UpdateLib.Data;
|
|
using UpdateLib.UI;
|
|
|
|
namespace UpdateLib
|
|
{
|
|
public class Updater : Component
|
|
{
|
|
private const String localUpdateFile = @".\UpdateInfo";
|
|
public String UpdateUrl { get; set; }
|
|
|
|
public void CheckForUpdates()
|
|
{
|
|
try
|
|
{
|
|
CleanUp();
|
|
WebClient downloadclient = new WebClient();
|
|
downloadclient.DownloadFile(UpdateUrl, localUpdateFile);
|
|
downloadclient.Dispose();
|
|
|
|
if(!File.Exists(localUpdateFile))
|
|
{
|
|
throw new FileNotFoundException("Lokale Update datei ist beschädigt");
|
|
}
|
|
|
|
UpdateSaveFile localFile = DecodeSaveFile(localUpdateFile);
|
|
|
|
Version localVersion = Assembly.GetEntryAssembly().GetName().Version;
|
|
Version onlineVersion = Version.Parse(localFile.VersionString);
|
|
|
|
if(onlineVersion > localVersion)
|
|
{
|
|
string MsgBox = String.Format("Version {0} verfügbar",onlineVersion);
|
|
if(MessageBox.Show(MsgBox,"Update",MessageBoxButtons.YesNo) == DialogResult.Yes)
|
|
{
|
|
frmUpdater updateform = new frmUpdater(localFile,GetPath(UpdateUrl));
|
|
updateform.ShowDialog();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(onlineVersion.ToString());
|
|
MessageBox.Show(localVersion.ToString());
|
|
MessageBox.Show("Software ist in der aktuellste Version");
|
|
}
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
//MessageBox.Show("Fehler beim uberprüfen von Updates\nVersuchen Sie später nochmal!\n\nFehlertext: " + e.Message, "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private string GetPath(string UpdateUrl)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
String[] updatePieces = UpdateUrl.Split('/');
|
|
for(int i = 0; i < updatePieces.Length -1; i++)
|
|
{
|
|
sb.Append(updatePieces[i] + "/");
|
|
}
|
|
return sb.ToString();
|
|
}
|
|
|
|
private UpdateSaveFile DecodeSaveFile(string localUpdateFile)
|
|
{
|
|
FileStream localFileStream = null;
|
|
BinaryFormatter decoder = null;
|
|
|
|
try
|
|
{
|
|
localFileStream = File.Open(localUpdateFile, FileMode.Open, FileAccess.Read);
|
|
decoder = new BinaryFormatter();
|
|
return (UpdateSaveFile)decoder.Deserialize(localFileStream);
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
throw new InvalidDataException("Der lokale updatedatei ist beschädigt "+e.Message);
|
|
}
|
|
finally
|
|
{
|
|
if (localFileStream != null)
|
|
localFileStream.Dispose();
|
|
}
|
|
}
|
|
|
|
|
|
private void CleanUp()
|
|
{
|
|
DirectoryInfo di = new DirectoryInfo(Application.StartupPath);
|
|
foreach (FileInfo fi in di.GetFiles("*.old", SearchOption.TopDirectoryOnly))
|
|
fi.Delete();
|
|
|
|
}
|
|
}
|
|
}
|