You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

124 lines
4.3 KiB

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Schema;
using System.IO.Compression;
using System.IO;
using System.Linq.Expressions;
namespace OnDocUpdate
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
}
public void Update() {
try
{
Application.DoEvents();
string filename = Params.UpdatePath + @"\version.txt";
string version = "";
if (System.IO.File.Exists(filename))
{
version = System.IO.File.ReadAllText(filename);
if (version != Params.Version)
{
MessageBox.Show("Die angeforderte Update-Version " + Params.Version + " ist nicht verfügbar", "OnDoc-Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
string sourcefilename = Params.UpdatePath + @"\" + Params.Version.Replace(".", "_") + "_OnDocUpdate.zip";
string destfilename = Params.DestPath + @"\" + Params.Version.Replace(".", "_") + "_OnDocUpdate.zip";
if (!System.IO.File.Exists(sourcefilename))
{
MessageBox.Show("Die angeforderte Update-Datei " + sourcefilename + " ist nicht vorhanden", "OnDoc-Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
return;
}
if (!System.IO.Directory.Exists(Params.DestPath))
{
MessageBox.Show("Die angegebene Zielverzeichnis " + Params.DestPath + " ist nicht vorhanden", "OnDoc-Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
System.IO.File.Copy(sourcefilename, destfilename, true);
if (ExtractAndOverwrite(destfilename, Params.DestPath))
{
if (Params.StartApp != "")
{
Process.Start(Params.StartApp);
}
}
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private bool ExtractAndOverwrite(string zipfile, string destpath)
{
try
{
string zipPath = zipfile;
string tempPath = Path.Combine(destpath, "tempUnzip");
string extractPath = destpath;
if (!System.IO.Directory.Exists(tempPath))
{
System.IO.Directory.CreateDirectory(tempPath);
}
ZipFile.ExtractToDirectory(zipPath, tempPath);
//build an array of the unzipped files
string[] files = Directory.GetFiles(tempPath);
foreach (string file in files)
{
FileInfo f = new FileInfo(file);
//Check if the file exists already, if so delete it and then move the new file to the extract folder
if (File.Exists(Path.Combine(extractPath, f.Name)))
{
File.Delete(Path.Combine(extractPath, f.Name));
File.Move(f.FullName, Path.Combine(extractPath, f.Name));
}
else
{
File.Move(f.FullName, Path.Combine(extractPath, f.Name));
}
}
//Delete the temporary directory.
Directory.Delete(tempPath);
return true;
}
catch (Exception)
{
return false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
Update();
this.Close();
}
}
}