Advertisement
Guest User

updater

a guest
Mar 6th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.IO.Compression;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Collections;
  12. using System.Xml;
  13. using System.Net;
  14. using System.Net.NetworkInformation;
  15. using System.Net.Sockets;
  16.  
  17. /*
  18.  
  19.  _____                                        _   _____ _     _                          
  20. |  __ \                                      (_) /  ___(_)   | |                          
  21. | |  \/_   _  __ _ _ __ __ _ _ __   __ _ _ __ _  \ `--. _ ___| |_ ___ _ __ ___   __ _ ___
  22. | | __| | | |/ _` | '__/ _` | '_ \ / _` | '__| |  `--. \ / __| __/ _ \ '_ ` _ \ / _` / __|
  23. | |_\ \ |_| | (_| | | | (_| | |_) | (_| | |  | | /\__/ / \__ \ ||  __/ | | | | | (_| \__ \
  24.  \____/\__,_|\__,_|_|  \__,_| .__/ \__,_|_|  |_| \____/|_|___/\__\___|_| |_| |_|\__,_|___/
  25.                             | |                                                          
  26.                             |_|                                                          
  27.                                 Powered By Luck | First compile 05/05/2016                                              
  28.  
  29. */
  30.  
  31. namespace ConsoleApplication1
  32. {
  33.     class Program
  34.     {
  35.         /*Classe de verificacao de processos*/
  36.         public static int verificaProcesso()
  37.         {
  38.             string sysmocom = "SysmoCom";//Nome do processo a ser descoberto
  39.             string ftpserver = "FTPServ";
  40.             string sysmovs = "sysmovs";
  41.             int processo = 0;
  42.             try
  43.             {
  44.                 var processss = from proc in System.Diagnostics.Process.GetProcesses() orderby proc.ProcessName ascending select proc;
  45.                 System.Threading.Thread.Sleep(10000);
  46.                 Console.WriteLine("\n\n Verificando processos abertos.... \n\n");
  47.                 foreach (var item in processss)//Lista todos processos
  48.                 {
  49.                     //   Console.WriteLine(item.ProcessName);
  50.                     if (item.ProcessName == sysmocom){processo = 1;} else if (item.ProcessName == ftpserver) { processo = 1; }else if(item.ProcessName == sysmovs) {
  51.                         Console.WriteLine("Sysmo S1 Está aberto! \n Fechando...");
  52.                         Process.Start("taskkill", "/F /IM "+ sysmovs + ".exe");
  53.                     }
  54.                 }
  55.             }
  56.             catch{}
  57.             return processo;
  58.         }
  59.  
  60.         public static string localIPAddress()
  61.         {
  62.             IPHostEntry host;
  63.             string localIP = "";
  64.             host = Dns.GetHostEntry(Dns.GetHostName());
  65.  
  66.             foreach (IPAddress ip in host.AddressList)
  67.             {
  68.                 localIP = ip.ToString();
  69.                 string[] temp = localIP.Split('.');
  70.                 if (ip.AddressFamily == AddressFamily.InterNetwork && temp[0] == "192") { break; } else {localIP = null;}
  71.             }
  72.             return localIP;
  73.         }
  74.  
  75.         static void Main(string[] args)
  76.         {
  77.             //   System.IO.File.Copy(@"\\192.168.1.249\completo\versao.xml", "c:\\sysmovs\\", true);
  78.             string ultimoUp = "";
  79.             XmlDocument xml = new XmlDocument();
  80.             xml.Load(@"\\192.168.1.249\completo\atualizacaosysmo\versao.xml");
  81.             XmlNodeList xnList = xml.SelectNodes("/config/versao");
  82.             foreach (XmlNode xn in xnList){ultimoUp = xn["ultimoUp"].InnerText;}
  83.             Console.WriteLine("\nData atualizacao Update: {0} \n", ultimoUp);
  84.  
  85.             string diaUp = ultimoUp.Substring(0, 2);
  86.             string mesUp = ultimoUp.Substring(3, 2);
  87.             string anoUp = ultimoUp.Substring(6, 4);
  88.             string diaUpOld = "";
  89.             string mesUpOld = "";
  90.             string anoUpOld = "";
  91.             int verifica = 0;
  92.             int temporizador = 5;//temporizador da finalização do update
  93.             string versao = "1.0";
  94.  
  95.             //Console.WriteLine("Dia {0} Mes {1} Ano {2}", diaUp, mesUp, anoUp);
  96.  
  97.             if (File.Exists(@"c:\sysmovs\versao.xml"))
  98.             {
  99.                 Console.WriteLine("Verifica data");
  100.                 string ultimoUpOld = "";
  101.                 XmlDocument xmlold = new XmlDocument();
  102.                 xmlold.Load(@"c:\sysmovs\versao.xml");
  103.                 XmlNodeList xnListold = xmlold.SelectNodes("/config/versao");
  104.                 foreach (XmlNode xns in xnListold) { ultimoUpOld = xns["ultimoUp"].InnerText; }
  105.                 Console.WriteLine("data Ultimo Update: {0} ", ultimoUpOld);
  106.                 diaUpOld = ultimoUpOld.Substring(0, 2);
  107.                 mesUpOld = ultimoUpOld.Substring(3, 2);
  108.                 anoUpOld = ultimoUpOld.Substring(6, 4);
  109.                // Console.WriteLine("Dia {0} Mes {1} Ano {2}", diaUp, mesUp, anoUp);
  110.             }
  111.             else {
  112.                 Console.WriteLine("Não tem copiando...");
  113.                 System.IO.File.Copy(@"\\192.168.1.249\completo\atualizacaosysmo\versao.xml", @"C:\sysmovs\versao.xml", true);
  114.                 Console.WriteLine("Verifica data");
  115.                 string ultimoUpOld = "";
  116.                 XmlDocument xmlold = new XmlDocument();
  117.                 xmlold.Load(@"c:\sysmovs\versao.xml");
  118.                 XmlNodeList xnListold = xmlold.SelectNodes("/config/versao");
  119.                 foreach (XmlNode xns in xnListold) { ultimoUpOld = xns["ultimoUp"].InnerText; }
  120.                 Console.WriteLine("data Ultimo Update: {0} ", ultimoUpOld);
  121.  
  122.                 diaUpOld = ultimoUpOld.Substring(0, 2);
  123.                 mesUpOld = ultimoUpOld.Substring(3, 2);
  124.                 anoUpOld = ultimoUpOld.Substring(6, 4);
  125.                 verifica = 1;
  126.             }
  127.  
  128.             DateTime date1 = new DateTime(Int32.Parse(anoUp), Int32.Parse(mesUp), Int32.Parse(diaUp), 0, 0, 0);
  129.             DateTime date2 = new DateTime(Int32.Parse(anoUpOld), Int32.Parse(mesUpOld), Int32.Parse(diaUpOld), 0, 0, 0);
  130.             int result = DateTime.Compare(date1, date2);
  131.             //Verifica relacao entre datas
  132.            if (verifica == 0) {
  133.                 Console.WriteLine("Entrou1");
  134.                 string relationship;
  135.                 if (result < 0) {
  136.  
  137.                     relationship = "é mais antigo que"; Environment.Exit(0);
  138.  
  139.                 }
  140.                     else {
  141.                         if (result == 0){
  142.                         Console.WriteLine("Entrou2");
  143.                         if (verifica == 1) {
  144.                                 Console.WriteLine("Primeiro Update com o Sysmo atualizador");
  145.                             }else{
  146.                             Console.WriteLine("Entrou3");
  147.                             relationship = "Seu S1 Já está atualizado.";
  148.                             Console.WriteLine(relationship);
  149.                             System.Threading.Thread.Sleep(5000); Environment.Exit(0); Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
  150.                         }
  151.  
  152.                     }
  153.                     else { relationship = "é mais novo que"; Console.WriteLine("{0} {1} {2}", date1, relationship, date2);}
  154.                 }
  155.             }
  156.             Console.ResetColor();
  157.             Console.WriteLine("=================Sysmo S1 Atualizador {0}=============",versao);
  158.             Type stypez = typeof(ConsoleColor);
  159.             Console.ForegroundColor = ConsoleColor.Red;
  160.             Console.WriteLine(" _____                                        _   _____ _     _       ");
  161.             Console.WriteLine(@"|  __ \                                      (_) /  ___(_)   | |     ");
  162.             Console.WriteLine(@"| |  \/_   _  __ _ _ __ __ _ _ __   __ _ _ __ _  \ `--. _ ___| |_ ___ _ __ ___   __ _ ___ ");
  163.             Console.WriteLine(@"| | __| | | |/ _` | '__/ _` | '_ \ / _` | '__| |  `--. \ / __| __/ _ \ '_ ` _ \ / _` / __|");
  164.             Console.WriteLine(@"| |_\ \ |_| | (_| | | | (_| | |_) | (_| | |  | | /\__/ / \__ \ ||  __/ | | | | | (_| \__ \");
  165.             Console.WriteLine(@" \____/\__,_|\__,_|_|  \__,_| .__/ \__,_|_|  |_| \____/|_|___/\__\___|_| |_| |_|\__,_|___/");
  166.             Console.WriteLine("                            | |                                                           ");
  167.             Console.WriteLine("                            |_|                                                           ");
  168.             Console.ResetColor();
  169.             Console.WriteLine("=================Sysmo S1 Atualizador {0}=============", versao);
  170.             Type stype1 = typeof(ConsoleColor);
  171.             Console.ForegroundColor = ConsoleColor.White;
  172.             //Console.WriteLine("Nenhuma nova atualização encontrada.");
  173.             Console.WriteLine("Encontramos uma nova atualização de seu sistema\n\n {0} ",ultimoUp);
  174.             Type stype2 = typeof(ConsoleColor);
  175.             Console.ForegroundColor = ConsoleColor.Red;
  176.             Console.BackgroundColor = ConsoleColor.White;
  177.             System.Threading.Thread.Sleep(2000);
  178.             Console.WriteLine("NÃO FECHE ESTA JANELA ESTAMOS ATUALIZANDO SEU SYSMO S1\n\n");
  179.             Console.ResetColor();
  180.             Console.WriteLine("Começando processo de update");
  181.  
  182.             string fileName = "versoes.zip";
  183.             string sourcePath = @"\\192.168.1.249\completo\atualizacaosysmo";
  184.             string targetPath = @"C:\Users\Public\";
  185.             string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
  186.             string destFile = System.IO.Path.Combine(targetPath, fileName);
  187.             string sysmofolder = @"c:\sysmovs\";
  188.  
  189.             FileInfo tamanho = new FileInfo(sourcePath.ToString() + "\\" + fileName.ToString());
  190.             Console.WriteLine("Tamanho da atualização {0} MB", ((tamanho.Length) / 1024) / 1024);//verifica o tamanho do update
  191.  
  192.             while (verificaProcesso() == 1) {
  193.                 DateTime dateTime = DateTime.Now;
  194.                 Console.WriteLine(dateTime);
  195.                 string sub = dateTime.ToString().Substring(0, 2);
  196.                 string subh = dateTime.ToString().Substring(11,2);
  197.                 int h = Int32.Parse(subh);
  198.                 int d = Int32.Parse(sub);
  199.                 int senha = d * h;
  200.                 Type stype = typeof(ConsoleColor);
  201.                 Console.ForegroundColor = ConsoleColor.Red;
  202.                 Console.WriteLine("\n\nEstá aberto o sysmocom ou FTPServer\n Verifique por favor!\n\n SENHA: " + senha);
  203.                 System.Threading.Thread.Sleep(5000);
  204.             }
  205.             Console.ResetColor();
  206.             if (!System.IO.Directory.Exists(targetPath))
  207.             {
  208.                 Console.WriteLine("Criando diretorio (" + targetPath + ")");
  209.                 System.IO.Directory.CreateDirectory(targetPath);
  210.              
  211.             }
  212.             Console.WriteLine("\n----------------------------------------------------------\n");
  213.             Console.WriteLine("Aguarde Estamos copiando a atualização... ("+fileName+")");
  214.             Console.WriteLine("\n----------------------------------------------------------\n");
  215.  
  216.             System.IO.File.Copy(sourceFile, destFile, true);
  217.             FileInfo arquivo = new FileInfo(destFile);
  218.  
  219.             while (arquivo.Length < tamanho.Length) { Console.WriteLine("Tamanho da atualização {0} MB", ((arquivo.Length) / 1024) / 1024); }//verifica o tamanho do update        
  220.             if (!System.IO.Directory.Exists(sysmofolder)){Console.WriteLine("Criando diretorio (" + sysmofolder + ")");  System.IO.Directory.CreateDirectory(sysmofolder); }
  221.             Console.WriteLine("Descompatando atualização em "+ sysmofolder);
  222.  
  223.             System.IO.Compression.ZipArchive archive = ZipFile.OpenRead(targetPath + fileName);
  224.             //Extrair arquivos
  225.             foreach (ZipArchiveEntry entry in archive.Entries) { Console.WriteLine("Instalando atualização {0}", entry); entry.ExtractToFile(Path.Combine(sysmofolder, entry.FullName), true); }
  226.  
  227.             Console.WriteLine("Atualizando Versao.xml");
  228.             System.IO.File.Copy(@"\\192.168.1.249\completo\atualizacaosysmo\versao.xml", @"C:\sysmovs\versao.xml", true);//ataulizando versoes
  229.             Console.ResetColor();
  230.             Type typez = typeof(ConsoleColor);
  231.             Console.ForegroundColor = ConsoleColor.Green;
  232.             Console.WriteLine("\n\n=================================\n \n Atualização Concluida com sucesso \n\n ================================= ");
  233.             try
  234.             {
  235.                 string Ip = localIPAddress();//localIPAddress(); Pega o Ip da maquina
  236.                 string finalIp = Ip.Substring(10, 3);
  237.                 Console.WriteLine(finalIp);
  238.                 int ipFinal = Int32.Parse(finalIp);
  239.  
  240.                 if (ipFinal == 100)
  241.                 {
  242.                     Console.WriteLine("Abrindo FTP SERVER E SYSMOCOM.");
  243.                     try{    Process.Start(@"c:\sysmovs\FTPServ.exe");}
  244.                     catch{  Console.WriteLine("FTPServ Não encontrado");}
  245.                     try{    Process.Start(@"c:\sysmovs\SysmoCom.exe");}
  246.                     catch{  Console.WriteLine("SysmoCom Não encontrado");}
  247.  
  248.                 }
  249.                 Console.WriteLine("Continuando instalação.");
  250.  
  251.             }
  252.             catch { Console.WriteLine("Este computador é cliente."); }
  253.  
  254.             for (int i = temporizador; i >= 0; i--)
  255.             {
  256.                 Console.WriteLine("Finalizando em Instalação em {0}", i);
  257.                 System.Threading.Thread.Sleep(2000);
  258.             }
  259.  
  260.             Environment.Exit(0);//Fecha o Console
  261.             //Console.ReadKey(); Precione uma tecla para continuar
  262.         }
  263.     }
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement