protected virtual void OnProgress(ProgressEventArgs e) { ProgressDownload?.Invoke(this, e); }
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); }