private void OnChanged(object source, FileSystemEventArgs e) { try { string filePath = e.FullPath; DateTime writeDate = File.GetLastWriteTime(filePath); if (lastWriteDate.ContainsKey(filePath)) { if (lastWriteDate[filePath] == writeDate) { return; } lastWriteDate[filePath] = writeDate; } else { lastWriteDate.Add(filePath, writeDate); } if (e.ChangeType == WatcherChangeTypes.Deleted) { Send(new Document { Name = Path.GetFileName(filePath), Type = e.ChangeType, }); } else { var content = ReadFile(filePath); Send(new Document { Name = Path.GetFileName(filePath), Type = e.ChangeType, Modified = File.GetLastWriteTime(filePath), Checksum = CryptTools.GetHashString(content), Content = content }); } } catch (Exception ex) { if (ex.Message.Contains("File timeout")) { string filePath = e.FullPath; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} - {1}: {2}", DateTime.Now.ToUniversalTime(), Path.GetFileName(filePath), "Cannot open file because it is being used by another process."); Console.ResetColor(); } } }
private void SendAll() { var files = Directory.GetFiles(PathToSync); foreach (var file in files) { var content = ReadFile(file); var document = new Document { Name = Path.GetFileName(file), Modified = File.GetLastWriteTime(file), Checksum = CryptTools.GetHashString(content), Content = content }; Send(document); } }
public Task Listen(CancellationToken cancellationToken) { return(new Task(() => { Console.WriteLine("-> Start Listening."); while (true) { if (cancellationToken.IsCancellationRequested) { Console.WriteLine("-> Stop listening."); break; } else { string received; NetworkStream stream = Client.GetStream(); while (stream.DataAvailable) { byte[] data = new byte[1000000]; using (MemoryStream ms = new MemoryStream()) { int numBytesRead; while (stream.DataAvailable && (numBytesRead = stream.Read(data, 0, data.Length)) > 0) { ms.Write(data, 0, numBytesRead); } received = Encoding.Unicode.GetString(ms.ToArray(), 0, (int)ms.Length); List <Document> documents = new List <Document>(); if (received.Where(x => x == '}').Count() > 1) { Regex regex = new Regex(@"(\})(\{)", RegexOptions.Multiline); received = regex.Replace(received, @"$1,$2"); received = $"[{received}]"; documents.AddRange(JsonConvert.DeserializeObject <IList <Document> >(received)); } else { documents.Add(JsonConvert.DeserializeObject <Document>(received)); } foreach (var document in documents) { var syncType = ""; if (document.Type.ToString() == "0") { syncType = "received"; } else { syncType = document.Type.ToString(); } Console.WriteLine("{0} - {1}: {2}", DateTime.Now.ToUniversalTime(), syncType, document.Name); if (document.ResendAll) { SendAll(); } else { var newFilePath = $"{PathToSync}/{document.Name}"; if (document.Type == WatcherChangeTypes.Deleted) { if (File.Exists(newFilePath)) { File.Delete(newFilePath); CanSend = false; } } else if (document.Type == WatcherChangeTypes.Renamed) { if (File.Exists($"{PathToSync}/{document.OldName}")) { File.Move($"{PathToSync}/{document.OldName}", newFilePath); CanSend = false; } } else { if (CryptTools.GetHashString(document.Content) != document.Checksum) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("{0} - CHECKSUM FAIL: {1}", DateTime.Now.ToUniversalTime(), document.Name); Console.ResetColor(); } else { if (!File.Exists(newFilePath)) { File.WriteAllBytes(newFilePath, document.Content); CanSend = false; } else { var currentModified = File.GetLastWriteTime(newFilePath); if (currentModified < document.Modified) { File.WriteAllBytes(newFilePath, document.Content); CanSend = false; } } } } } } } } Thread.Sleep(1000); } } }, cancellationToken)); }