Auto updater added

This commit is contained in:
goomatt33
2023-11-20 16:20:20 -05:00
parent 87a73ff24b
commit 8cff012772
23 changed files with 776 additions and 33 deletions

View File

@@ -0,0 +1,45 @@
using System;
using System.IO;
using System.Net;
namespace PWAppUpdater.FTP
{
class FTPManager
{
private string BaseUri;
private NetworkCredential credential;
public FTPManager(string baseUri, string username, string password)
{
BaseUri = baseUri;
credential = new NetworkCredential(username, password);
}
public void DownloadFile(string FTPPath, string localPath)
{
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(BaseUri + FTPPath);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = credential;
request.KeepAlive = false;
request.UseBinary = true;
request.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
using (Stream s = File.Create(localPath))
{
responseStream.CopyTo(s);
}
response.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
}
}
}

98
PWAppUpdater2/Form1.Designer.cs generated Normal file
View File

@@ -0,0 +1,98 @@
namespace PWAppUpdater2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
progressBar1 = new ProgressBar();
button1 = new Button();
label1 = new Label();
StepInfo = new Label();
SuspendLayout();
//
// progressBar1
//
progressBar1.Location = new Point(30, 60);
progressBar1.Name = "progressBar1";
progressBar1.Size = new Size(200, 10);
progressBar1.TabIndex = 0;
progressBar1.Click += progressBar1_Click;
//
// button1
//
button1.Location = new Point(105, 80);
button1.Name = "button1";
button1.Size = new Size(50, 23);
button1.TabIndex = 1;
button1.Text = "OK";
button1.UseVisualStyleBackColor = true;
button1.Click += button1_Click;
//
// label1
//
label1.AutoSize = true;
label1.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point);
label1.Location = new Point(12, 9);
label1.Name = "label1";
label1.Size = new Size(168, 30);
label1.TabIndex = 2;
label1.Text = "The program will now update. \r\nPlease click \"OK\" to continue.";
label1.Click += label1_Click;
//
// StepInfo
//
StepInfo.AutoSize = true;
StepInfo.Location = new Point(30, 42);
StepInfo.Name = "StepInfo";
StepInfo.Size = new Size(0, 15);
StepInfo.TabIndex = 3;
StepInfo.Click += label2_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(264, 111);
Controls.Add(StepInfo);
Controls.Add(label1);
Controls.Add(button1);
Controls.Add(progressBar1);
Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
PerformLayout();
}
#endregion
private ProgressBar progressBar1;
private Button button1;
private Label label1;
private Label StepInfo;
}
}

51
PWAppUpdater2/Form1.cs Normal file
View File

@@ -0,0 +1,51 @@
using PWAppUpdater;
namespace PWAppUpdater2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "Updating.... Please wait.";
label1.Update();
progressBar1.Maximum = 8;
progressBar1.Step = 1;
button1.Enabled = false;
try
{
PWAppUpdater.Updater.Update("C:\\PWAPP\\App\\", progressBar1, StepInfo, "C:\\PWAPP\\Temp\\");
}
catch (NoAuthException)
{
MessageBox.Show("Please run the updater with administrator privilages or from the PWApp Application.");
Application.Exit();
}
MessageBox.Show("Done!");
Application.Exit();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}

120
PWAppUpdater2/Form1.resx Normal file
View File

@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>

17
PWAppUpdater2/Program.cs Normal file
View File

@@ -0,0 +1,17 @@
namespace PWAppUpdater2
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}

77
PWAppUpdater2/Updater.cs Normal file
View File

@@ -0,0 +1,77 @@
using PWAppUpdater.FTP;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace PWAppUpdater
{
class Updater
{
public static void Update(string installPath, ProgressBar progress, Label stepInfo, string tempPath = "")
{
try
{
progress.PerformStep();
stepInfo.Text = "Creating temp directories...";
stepInfo.Update();
if (tempPath == "")
{
tempPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
tempPath = Path.Combine(tempPath, "PWAPP\\Temp");
}
progress.PerformStep();
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
progress.PerformStep();
stepInfo.Text = "Downloading update...";
stepInfo.Update();
FTP.FTPManager ftpManager = new FTPManager("ftp://waws-prod-blu-109.ftp.azurewebsites.windows.net/", "patientweb\\$patientweb", "vHBnkgxPDS4Q410eehaFlXb8DH67QW50m9Rsxf1omXyYWRDgYioWJL63Tagp");
ftpManager.DownloadFile("pwapp/current/Release.zip", tempPath + "\\Release.zip");
progress.PerformStep();
stepInfo.Text = "Removing old installation...";
stepInfo.Update();
if (Directory.Exists(installPath))
{
Directory.Delete(installPath, true);
}
progress.PerformStep();
stepInfo.Text = "Creating new install directory...";
stepInfo.Update();
Directory.CreateDirectory(installPath);
progress.PerformStep();
stepInfo.Text = "Unpacking update...";
stepInfo.Update();
Zip.ZipManager.UnZip(tempPath + "\\Release.zip", installPath, true);
progress.PerformStep();
stepInfo.Text = "Cleaning up installation...";
stepInfo.Update();
Directory.Delete(tempPath, true);
progress.PerformStep();
stepInfo.Text = "Complete!";
stepInfo.Update();
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Updater does not have sufficent permissions to perform update. Please run with administrator privilages to continue.");
throw new NoAuthException();
}
}
}
class NoAuthException : Exception { }
class CouldNotFindInstallPathException : Exception
{
}
}

View File

@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PWAppUpdater.Zip
{
class ZipManager
{
public static void UnZip(string source, string destination, bool Override)
{
if (Override)
{
if (Directory.Exists(destination))
{
Directory.Delete(destination, true);
}
}
ZipFile.ExtractToDirectory(source, destination);
}
}
}