Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /// <summary>
- /// Služi za spajanje na server te uspostavljanje FTP konekcije.
- /// Gleda se txtFile lastVersion.txt sa servera, uspoređuje se s txtFileom currentVersion.txt na korisnikovom računalu
- /// Ako je lastVersion.txt s servera != od currentVersion.txt na korisnikovom računalu, tada
- /// skini novu verziju programa i obavijesti korisnika da je skinuta najnovija verzija
- ///
- /// AKO currentVersion.txt NE postoji na korisnikovom racunalu, tada bude isti slucaj kao
- /// i ako je currentVersion.txt != lastVersion.txt sa servera
- /// </summary>
- private void ConnectToFtp()
- {
- try
- {
- //Za preuzimanje lastVersion.txt s servera
- GetTxtLastVersion();
- //Za PC POS.exe
- if (VersionsDiffer())
- {
- GetPCPosNewVersion();
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- //Ova metoda provjerava ukoliko se verzija koja je na serveru razlikuje od one koja je na računalu
- private bool VersionsDiffer()
- {
- //Paths
- string currentPathLastVersion = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"lastVersion.txt");
- string currentPathCurrentVersion = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"currentVersion.txt");
- string currentPathPCPos = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"PC POS.exe");
- //Ako ne postoji currentVersion.txt, dupliciraj lastVersion.txt s imenom currentVersion.txt, return true -> preuzimanje nove verzije PC POS.exe
- if (!File.Exists(currentPathCurrentVersion))
- {
- System.IO.File.Copy(currentPathLastVersion, currentPathCurrentVersion);
- return true;
- }
- else
- //Inače postoji currentVersion.txt
- {
- //Izvadi prvi redak iz currentVersion.txt
- string currentVersion;
- using (StreamReader reader = new StreamReader(currentPathCurrentVersion))
- {
- currentVersion = reader.ReadLine() ?? "";
- }
- //Izvadi prvi redak iz nedavno preuzetog lastVersion.txt
- string lastVersion;
- using (StreamReader reader = new StreamReader(currentPathLastVersion))
- {
- lastVersion = reader.ReadLine() ?? "";
- }
- //Usporedi ta 2 retka
- if (currentVersion.Equals(lastVersion))
- {
- MessageBox.Show("Na Vašem računalu nalazi se najnovija verzija.", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return false;
- }
- else
- {
- //Updateaj verziju u currentVersion.txt
- System.IO.File.Copy(currentPathLastVersion, currentPathCurrentVersion,true);
- return true;
- }
- }
- }
- //This method gets lastVersion.txt
- private void GetTxtLastVersion()
- {
- string fileName = @"lastVersion.txt";
- string url = $"ftp://5.189.154.50/CodeCaffe/{fileName}";
- using (WebClient req = new WebClient())
- {
- req.Credentials = new NetworkCredential("codeadmin", "Eqws64%2");
- byte[] fileData = req.DownloadData(url);
- using (FileStream file = File.Create(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"{fileName}")))
- {
- file.Write(fileData, 0, fileData.Length);
- }
- }
- }
- //This method gets PCPos New Version
- private void GetPCPosNewVersion()
- {
- string fileName = @"PC POS.exe";
- string url = $"ftp://5.189.154.50/CodeCaffe/{fileName}";
- using (WebClient req = new WebClient())
- {
- req.Credentials = new NetworkCredential("codeadmin", "Eqws64%2");
- byte[] fileData = req.DownloadData(url);
- using (FileStream file = File.Create(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"{fileName}")))
- {
- file.Write(fileData, 0, fileData.Length);
- }
- }
- MessageBox.Show("Nova verzija je preuzeta. Molimo vas da ponovno pokrenete program.", "Informacija", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement