Wie kann man einen Updater in C# programmieren?
1 Antwort
Von gutefrage auf Grund seines Wissens auf einem Fachgebiet ausgezeichneter Nutzer
programmieren
Ungefähr so:
Auf Clientseite:
using System;
using System.Net;
using System.IO;
using System.IO.Compression;
public class Updater{
private const string Name = "MyApp";
private const int Version = 12345;
private static DirectoryInfo InstallDir = new DirectoryInfo("C:\\Foo.Bar\\MyApp\\");
public static void Main(string[] args){
WebClient wc = new WebClient();
string UpdateLink = wc.DownloadString(string.Fhttp://updates.myapp.de/checkUpdate.php?version=ersion=",Version));
FileInfo UpdateFile = null;
if(!string.IsNullOrEmpty(UpdateLink)){
Uri UpdateUri = new Uri(UpdateLink);
UpdateFile = new FileInfo(UpdateUri.AbsolutePath);
UpdateFile = new FileInfo(Path.Combine(Path.GetTempPath(), UpdateFile.Name));
wc.DownloadFile(UpdateUri, UpdateFile.FullName);
}
ZipFile.ExtractToDirectory(UpdateFile.FullName, InstallDir.FullName);
}
}
Auf Serverseite:
<?php
$mostRecentVersion = 12345;
if(isset($_GET['version'])) $version = intVal($_GET['version']);
else exit(0);
if($version < $mostRecentVersion){
http://updates.myapp.de/MyApp-/MyApp-" . $mostRecentVersion . ".zip";
}
else{
exit(0);
}
ACHTUNG: Das ganze ist als grobe Skizze zu betrachten, nicht als 1:1 Bauplan.