Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. /*
  2. /// <summary>
  3. /// Služi za spajanje na server te uspostavljanje FTP konekcije.
  4. /// Gleda se txtFile lastVersion.txt sa servera, uspoređuje se s txtFileom currentVersion.txt na korisnikovom računalu
  5. /// Ako je lastVersion.txt s servera != od currentVersion.txt na korisnikovom računalu, tada
  6. /// skini novu verziju programa i obavijesti korisnika da je skinuta najnovija verzija
  7. ///
  8. /// AKO currentVersion.txt NE postoji na korisnikovom racunalu, tada bude isti slucaj kao
  9. /// i ako je currentVersion.txt != lastVersion.txt sa servera
  10. /// </summary>
  11. private void ConnectToFtp()
  12. {
  13. try
  14. {
  15. //Za preuzimanje lastVersion.txt s servera
  16. GetTxtLastVersion();
  17.  
  18. //Za PC POS.exe
  19. if (VersionsDiffer())
  20. {
  21. GetPCPosNewVersion();
  22. }
  23. }
  24. catch (Exception ex)
  25. {
  26. MessageBox.Show(ex.Message);
  27. }
  28. }
  29.  
  30. //Ova metoda provjerava ukoliko se verzija koja je na serveru razlikuje od one koja je na računalu
  31. private bool VersionsDiffer()
  32. {
  33. //Paths
  34. string currentPathLastVersion = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"lastVersion.txt");
  35. string currentPathCurrentVersion = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"currentVersion.txt");
  36. string currentPathPCPos = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"PC POS.exe");
  37.  
  38. //Ako ne postoji currentVersion.txt, dupliciraj lastVersion.txt s imenom currentVersion.txt, return true -> preuzimanje nove verzije PC POS.exe
  39. if (!File.Exists(currentPathCurrentVersion))
  40. {
  41. System.IO.File.Copy(currentPathLastVersion, currentPathCurrentVersion);
  42. return true;
  43. }
  44. else
  45. //Inače postoji currentVersion.txt
  46. {
  47. //Izvadi prvi redak iz currentVersion.txt
  48. string currentVersion;
  49. using (StreamReader reader = new StreamReader(currentPathCurrentVersion))
  50. {
  51. currentVersion = reader.ReadLine() ?? "";
  52. }
  53.  
  54. //Izvadi prvi redak iz nedavno preuzetog lastVersion.txt
  55. string lastVersion;
  56. using (StreamReader reader = new StreamReader(currentPathLastVersion))
  57. {
  58. lastVersion = reader.ReadLine() ?? "";
  59. }
  60.  
  61. //Usporedi ta 2 retka
  62. if (currentVersion.Equals(lastVersion))
  63. {
  64. MessageBox.Show("Na Vašem računalu nalazi se najnovija verzija.", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
  65. return false;
  66. }
  67. else
  68. {
  69. //Updateaj verziju u currentVersion.txt
  70. System.IO.File.Copy(currentPathLastVersion, currentPathCurrentVersion,true);
  71. return true;
  72. }
  73. }
  74. }
  75.  
  76.  
  77. //This method gets lastVersion.txt
  78. private void GetTxtLastVersion()
  79. {
  80. string fileName = @"lastVersion.txt";
  81. string url = $"ftp://5.189.154.50/CodeCaffe/{fileName}";
  82. using (WebClient req = new WebClient())
  83. {
  84. req.Credentials = new NetworkCredential("codeadmin", "Eqws64%2");
  85. byte[] fileData = req.DownloadData(url);
  86.  
  87. using (FileStream file = File.Create(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"{fileName}")))
  88. {
  89. file.Write(fileData, 0, fileData.Length);
  90. }
  91. }
  92. }
  93.  
  94. //This method gets PCPos New Version
  95. private void GetPCPosNewVersion()
  96. {
  97. string fileName = @"PC POS.exe";
  98. string url = $"ftp://5.189.154.50/CodeCaffe/{fileName}";
  99. using (WebClient req = new WebClient())
  100. {
  101. req.Credentials = new NetworkCredential("codeadmin", "Eqws64%2");
  102. byte[] fileData = req.DownloadData(url);
  103.  
  104. using (FileStream file = File.Create(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"{fileName}")))
  105. {
  106. file.Write(fileData, 0, fileData.Length);
  107. }
  108. }
  109.  
  110. MessageBox.Show("Nova verzija je preuzeta. Molimo vas da ponovno pokrenete program.", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
  111. }
  112. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement