Skip to content

Commit

Permalink
Fixed Updater
Browse files Browse the repository at this point in the history
# Updater wasn't really updating
  • Loading branch information
Kirdock committed Jan 11, 2019
1 parent 35090aa commit 5a4f712
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
86 changes: 61 additions & 25 deletions DataTableConverter/Assisstant/UpdateHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

Expand All @@ -15,43 +16,57 @@ class UpdateHelper
{
private readonly static string Repository = "https://github.com/Kirdock/Tabellenverarbeitung/releases";
private readonly static string Tags = $"{Repository}/latest";
private readonly static string FileName = "Anwendung.zip";
private readonly static string FileNameWithoutExtension = "Anwendung";
private readonly static string FileName = FileNameWithoutExtension+".zip";
private readonly static string Download = Repository+"/download/{0}/"+FileName;
internal static void CheckUpdate(Form1 Form)

internal static void CheckUpdate(bool Prompt)
{
try
{
WebRequest request = WebRequest.Create(Tags);
WebResponse response = request.GetResponse();
new Thread(() =>
{
WebRequest request = WebRequest.Create(Tags);
WebResponse response = request.GetResponse();

string version = response.ResponseUri.ToString().Split(new char[] { '/' }).Last();
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = response.ResponseUri.ToString().Split(new char[] { '/' }).Last();
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);

Console.WriteLine(version);
Console.WriteLine(fvi.FileVersion);
if (version != fvi.FileVersion.Substring(0, version.Length))
{
DownloadFile(version, Form);
}
else
{
MessageHandler.MessagesOK(System.Windows.Forms.MessageBoxIcon.Information, "Sie besitzen bereits die aktuelle Version");
}
}
catch(Exception ex)
if (version != fvi.FileVersion.Substring(0, version.Length))
{
if (Prompt)
{
DialogResult result = MessageHandler.MessagesYesNo(MessageBoxIcon.Information, "Eine neue Version steht zur Verfügung. Möchten Sie sie runterladen?");
if(result == DialogResult.Yes)
{
DownloadFile(version);
}
}
else
{
DownloadFile(version);
}
}
else if(!Prompt)
{
MessageHandler.MessagesOK(MessageBoxIcon.Information, "Sie besitzen bereits die aktuellste Version");
}
}).Start();
}
catch (Exception ex)
{
ErrorHelper.LogMessage(ex.ToString());
}
}

private static void DownloadFile(string version, Form1 Form)
private static void DownloadFile(string version)
{
using (var client = new WebClient())
{
client.DownloadFile(string.Format(Download,version), FileName);

string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path = GetCurrentDirectory();
string ZipPath = Path.Combine(path, FileName);
ZipArchive archive = ZipFile.OpenRead(ZipPath);

Expand All @@ -67,15 +82,36 @@ private static void DownloadFile(string version, Form1 Form)
}
archive.Dispose();
File.Delete(ZipPath);
RestartApp(Form);
RestartApp();

}
}

private static void RestartApp(Form1 Form)
private static string GetCurrentDirectory()
{
Process.Start(Application.ExecutablePath); // to start new instance of application
Form.Close();
return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
}

private static void RestartApp()
{
string directory = GetCurrentDirectory();
string BatchPath = Path.Combine(directory, "Updater.bat");
string ZipFolder = Path.Combine(directory, FileNameWithoutExtension);
StreamWriter writer = new StreamWriter(BatchPath);

writer.WriteLine($"move \"{Path.Combine(ZipFolder, "*")}\" \"{directory}\"");
writer.WriteLine($"start \"\" \"{Path.Combine(directory, AppDomain.CurrentDomain.FriendlyName)}\"");
writer.WriteLine($"rmdir \"{ZipFolder}\"");
writer.WriteLine($"del /Q \"{BatchPath}\"");

writer.Close();
Process process = new Process();
process.StartInfo.FileName = BatchPath;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
Application.Exit();

}
}
}
4 changes: 2 additions & 2 deletions DataTableConverter/View/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal Form1(DataTable table = null)
{
assignDataSource(table);
}
//UpdateHelper.CheckUpdate();
UpdateHelper.CheckUpdate(true);
}

private void setSorting(string order)
Expand Down Expand Up @@ -1167,7 +1167,7 @@ private void einstellungenToolStripMenuItem_Click(object sender, EventArgs e)

private void updateToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdateHelper.CheckUpdate(this);
UpdateHelper.CheckUpdate(false);
}

private void sortierenToolStripMenuItem_Click(object sender, EventArgs e)
Expand Down

0 comments on commit 5a4f712

Please sign in to comment.