Beispiel #1
0
        private HashSet <HashSet <string> > CheckHashes()
        {
            string hashes = Properties.Settings.Default.FileHash;
            Dictionary <string, string> serverHash = new Dictionary <string, string>();
            Dictionary <string, string> clientHash = new Dictionary <string, string>();

            try
            {
                FtpWebRequest DLrequest = (FtpWebRequest)WebRequest.Create(getFtpUrl() + "hashlist.txt");
                DLrequest.UseBinary   = true;
                DLrequest.KeepAlive   = false;
                DLrequest.Method      = WebRequestMethods.Ftp.DownloadFile;
                DLrequest.Credentials = getCredentials();
                Stream ftpStream = DLrequest.GetResponse().GetResponseStream();
                string sHashes   = new StreamReader(ftpStream).ReadToEnd();
                foreach (string entry in sHashes.Split(';'))
                {
                    string[] e = entry.Split('~');
                    if (!serverHash.ContainsKey(e[0]))
                    {
                        serverHash.Add(e[0], e[1]);
                    }
                }
            }
            catch (WebException excp)
            {
                CatchFTPExceptions(excp);
                return(new HashSet <HashSet <string> >());
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.ToString());
                return(new HashSet <HashSet <string> >());
            }

            if (true)
            {
                string           sDir   = TxtPath.Text;
                SHA1             sec    = SHA1.Create();
                HashSet <string> retVal = new HashSet <string>();

                if (DirSearch(sDir) == true)
                {
                    float fileCount = lstFilesFound.Count;
                    float count     = 0;
                    foreach (string file in lstFilesFound)
                    {
                        count++;
                        if (File.Exists(file) && file.Contains(@"@NTLauncherRPG"))
                        {
                            using (var stream = File.OpenRead(file))
                            {
                                var hash = sec.ComputeHash(stream);
                                retVal.Add(file.Replace(sDir + @"\", "") + "~" + BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant());

                                //change percentage
                                LblFilename.Invoke(new UpdateFileNameCallback(UpdateFileName), new object[] { LblFilename, file, true });
                                ProgressDownload.Invoke(new UpdateProgressCallback(UpdateProgress), new object[] { ProgressDownload, (int)Math.Round((count / fileCount) * 100) });
                            }
                        }
                    }

                    hashes = string.Join(";", retVal.Cast <string>().ToArray());
                    Properties.Settings.Default.FileHash = hashes;
                    Properties.Settings.Default.Save();
                }

                if (!hashes.Equals(""))
                {
                    clientHash = new Dictionary <string, string>();
                    foreach (string entry in hashes.Split(';'))
                    {
                        string[] e = entry.Split('~');
                        if (!clientHash.ContainsKey(e[0]))
                        {
                            clientHash.Add(e[0], e[1]);
                        }
                    }
                }
            }

            HashSet <string> toDownload = new HashSet <string>();
            HashSet <string> toDelete   = new HashSet <string>();

            foreach (KeyValuePair <string, string> kvp in serverHash)
            {
                string key   = kvp.Key;
                string value = kvp.Value;
                if (!clientHash.ContainsKey(key))
                {
                    toDownload.Add(key);
                }
                else if (!value.Equals(clientHash[key]))
                {
                    toDownload.Add(key);
                }
            }
            foreach (KeyValuePair <string, string> kvp in clientHash)
            {
                string key   = kvp.Key;
                string value = kvp.Value;
                if (!serverHash.ContainsKey(key))
                {
                    if (key.Contains(@"@NTLauncherRPG"))
                    {
                        toDelete.Add(key);
                    }
                }
            }
            foreach (string file in toDownload)
            {
                string path = getSavePath();
                if (File.Exists(path + file))
                {
                    toDelete.Add(file);
                }
            }

            HashSet <HashSet <string> > ret = new HashSet <HashSet <string> >();

            ret.Add(toDelete);
            ret.Add(toDownload);
            return(ret);
        }
Beispiel #2
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            try
            {
                HashSet <HashSet <string> > hash       = CheckHashes();
                HashSet <string>            toDelete   = hash.ElementAt(0);
                HashSet <string>            toDownload = hash.ElementAt(1);

                DeleteFiles(toDelete);
                worker.ReportProgress(0);

                //List<string> files = getAllFiles();
                string savePath = getSavePath();

                float progress = 0;
                float total    = toDownload.Count;
                int   percent  = 0;

                foreach (string file in toDownload)
                {
                    LblFilename.Invoke(new UpdateFileNameCallback(UpdateFileName), new object[] { LblFilename, file, false });

                    string subPath = Regex.Replace(file, @"\\([\w_-]*)\.\w*", "");
                    if (subPath.Equals(file))
                    {
                        subPath = "";
                    }
                    else
                    {
                        subPath += @"\";
                    }
                    string[]      path      = subPath.Split(Convert.ToChar(@"\"));
                    string        fileName  = Regex.Replace(file, @".*\w*\\", "");
                    FtpWebRequest DLrequest = (FtpWebRequest)WebRequest.Create(getFtpUrl() + file);

                    DLrequest.Method      = WebRequestMethods.Ftp.DownloadFile;
                    DLrequest.Credentials = getCredentials();

                    /*DLrequest.Proxy = null;
                     * DLrequest.UseBinary = true;
                     * DLrequest.UsePassive = false;*/

                    string temp = "";
                    foreach (string p in path)
                    {
                        string workingPath = savePath + temp + p;
                        if (!Directory.Exists(workingPath))
                        {
                            Directory.CreateDirectory(workingPath);
                        }

                        temp += p + @"\";
                    }

                    using (Stream ftpStream = DLrequest.GetResponse().GetResponseStream())
                        using (Stream fileStream = File.Create(savePath + subPath + fileName))
                        {
                            ftpStream.CopyTo(fileStream);
                        }

                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }

                    percent = (int)Math.Round((++progress / total) * 100);
                    worker.ReportProgress(percent);
                }

                PicStatus.Image = Properties.Resources.downloadFin;
            }
            catch (WebException webExcp)
            {
                ResetImage();
                CatchFTPExceptions(webExcp);
            }
            catch (DirectoryNotFoundException ex)
            {
                ResetImage();
                MessageBox.Show("Der eingegebene Pfad ist leer oder existiert nicht", "Fehler: Speicherpfad existiert nicht",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                Console.WriteLine("A FileNotFoundException has been caught.");
                Console.WriteLine(ex.ToString());
            }
            catch (UnauthorizedAccessException ex)
            {
                ResetImage();
                MessageBox.Show("Der angegebene Pfad erfordert höhere Berechtigungen.\n" +
                                "Bitte starte das Programm mit Administrator Berechtigngen neu oder wähle einen anderen Speicherpfad.",
                                "Fehler: keine Berechtigungen", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Console.WriteLine("A FileNotFoundException has been caught.");
                Console.WriteLine(ex.ToString());
            }
            catch (Exception ex)
            {
                ResetImage();
                Console.WriteLine("other exception caught!");
                Console.WriteLine(ex.ToString());
            }
        }