private void Updatethread(string host, int port) { string serverOrt = ""; string serverVers = ""; try { var updater = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); updater.Connect(host, port); var ns = new NetworkStream(updater); //hier der stream für das socket var bw = new BinaryWriter(ns); //klasse zum binären schreiben in einen stream var br = new BinaryReader(ns); // " " " lesen aus einem " var e = new UpdateEventArgs(null, false, false, null, null); if (updater.Connected) { bw.Write(3 + UserObjects.Count); //anzahl an zusatzinformationen + 3 die man sowieso hat bw.Write("ProgrammName"); // name der 1. info bw.Write(ProgramName); //daten der 1. info bw.Write("Version"); // name der 2. info ..... bw.Write(Version); bw.Write("ComputerName"); bw.Write(ComputerName); bw.Flush(); foreach (string k in UserObjects.Keys) { bw.Write(k); bw.Write(UserObjects[k]); bw.Flush(); } serverVers = br.ReadString(); //version lesen serverOrt = br.ReadString(); //url zum downloaden lesen bw.Close(); updater.Close(); //schließen if (serverVers != Version) //... { e = new UpdateEventArgs(serverOrt, true, true, "Update available.", serverVers); } else { e = new UpdateEventArgs(serverOrt, true, false, "No update available.", serverVers); } } else { e = new UpdateEventArgs(serverOrt, false, false, "Updateprogress failed. Don't know if an update is available.", serverVers); } if (UpdateAvialable != null) { UpdateAvialable(this, e); } else { StartBuiltInUpdate(e); } } catch (Exception e) { if (UpdateAvialable != null) { UpdateAvialable(this, new UpdateEventArgs(serverOrt, false, false, e.ToString(), serverVers)); } else { StartBuiltInUpdate(new UpdateEventArgs(serverOrt, false, false, e.ToString(), serverVers)); } } }
public void StartBuiltInUpdate(UpdateEventArgs e) { if (e.Url != "") { if (e.Success) { if (e.Available) { try { new WebClient().DownloadFile(e.Url, ProgramName + ".update"); var fi = new FileInfo(ProgramName + ".update"); var cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); cp.ReferencedAssemblies.Add("System.Xaml.dll"); cp.ReferencedAssemblies.Add("WindowsBase.dll"); cp.ReferencedAssemblies.Add("PresentationFramework.dll"); cp.ReferencedAssemblies.Add("PresentationCore.dll"); cp.GenerateExecutable = true; cp.CompilerOptions = string.Format("/target:winexe /lib:\"{0}\"", Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @"Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0")); //new string[] {"",""}.Any() CodeDomProvider cdp = CodeDomProvider.CreateProvider("c#"); Assembly assembly = Assembly.GetExecutingAssembly(); string resourceName = assembly.GetManifestResourceNames().Where(x => x.EndsWith("bRenamer.cs")).First(); var resourceStreamReader = new StreamReader(assembly.GetManifestResourceStream(resourceName)); string prog = resourceStreamReader.ReadToEnd(); CompilerResults Results = cdp.CompileAssemblyFromSource(cp, prog); Process.Start(Results.PathToAssembly, "\"" + fi.FullName + "\" \"" + Assembly.GetExecutingAssembly().Location + "\""); Dispatcher.Invoke((Action) delegate { Application.Current.MainWindow.Close(); }); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } } }