Zekrommaster110

[C#] Update Service Class Snippet

Jun 2nd, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using mouseCounter.Properties; //CHANGE NAMESPACE
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Diagnostics;
  11. using static mouseCounter.cConst; //CHANGE NAMESPACE
  12.  
  13. namespace mouseCounter //CHANGE NAMESPACE
  14. {
  15.     class cUpdate
  16.     {
  17.         public static string getOnelineFile(string URL)
  18.         {
  19.             WebClient client = new WebClient();
  20.             Stream stream = client.OpenRead(URL);
  21.             StreamReader reader = new StreamReader(stream);
  22.             return reader.ReadToEnd();
  23.         }
  24.  
  25.         public static bool getUpdateStatus(string clientVersion, string latestVersion)
  26.         {
  27.             if (clientVersion == latestVersion)
  28.                 return false;
  29.             else
  30.                 return true;
  31.         }
  32.  
  33.         public static void update()
  34.         {
  35.             if (getUpdateStatus(VERSION, getOnelineFile(versionFileURL)))
  36.             {
  37.                 var msgbox = MessageBox.Show("A update is available! \n\nClient version: " + VERSION + "\nLatest version: " + getOnelineFile(versionFileURL) + "\n\nDo you want to downlaod the lates version now?",
  38.                                              "Update available",
  39.                                              MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
  40.  
  41.                 if (msgbox == DialogResult.Yes)
  42.                 {
  43.                     /*
  44.                     cXMLdataSet set = new cXMLdataSet();
  45.                     set.leftMouseCounter = Settings.Default.leftMouseCounter.ToString();
  46.                     set.rightMouseCounter = Settings.Default.rightMouseCounter.ToString();
  47.                     cXMLSerializer.writeXML(set, "_update_data.xml");
  48.                     */
  49.  
  50.                     WebClient client = new WebClient();
  51.                     client.DownloadFile(getOnelineFile(downloadFileURL), "mouseCounterUpdate.exe");
  52.  
  53.                     StreamWriter w = new StreamWriter("updatescript.bat");
  54.                     w.WriteLine("@echo off");
  55.                     w.WriteLine("if exist mouseCounter.exe (");
  56.                     w.WriteLine("   del mouseCounter.exe");
  57.                     w.WriteLine("   ren mouseCounterUpdate.exe mouseCounter.exe");
  58.                     w.WriteLine("   start mouseCounter.exe");
  59.                     w.WriteLine(") else (");
  60.                     w.WriteLine("   echo You have probably renamed the file mouseCounter.exe so the update script can not find the file. Pelase rename it to the ortiginal name to continue!");
  61.                     w.WriteLine("   pause )");
  62.                     w.WriteLine("del updatescript.bat");
  63.                     w.Close();
  64.  
  65.                     Process.Start("updatescript.bat");
  66.                     Application.Exit();
  67.                 }
  68.             }
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment