internal void RestoreVersion(ListVersion version) { if (Subscriber == null) { return; } FileInfo fileInfoVersion = new FileInfo(version.Path); if (!DirFile.FileExists(fileInfoVersion)) { return; } FileInfo tempFileVersion = new FileInfo(DirFile.GetTempFile()); fileInfoVersion.CopyTo(tempFileVersion.FullName); tempFileVersion.LastWriteTime = DateTime.Now; FileInfo fileInfoRestore = new FileInfo(Subscriber.Path); tempFileVersion.CopyTo(fileInfoRestore.FullName, true); DirFile.DeleteFile(tempFileVersion); }
private void Subscribe_Changed(object sender, FileSystemEventArgs e) { try { Thread.Sleep(1000); FileInfo fileInfo = new FileInfo(e.FullPath); FileSubscriber subscriber = _listSubscriber.First(f => f.Key.FullName == fileInfo.FullName).Value; string tempFileName = DirFile.GetTempFile(); FileInfo fileInfoTemp = fileInfo.CopyTo(tempFileName); string fileName = fileInfo.Name; string extension = fileInfo.Extension; fileInfo = null; Task.Run(() => { lock (_locker) { using (Versions versions = new Versions() { SubscriberInfo = fileInfoTemp, Subscriber = subscriber, FileName = fileName, Extension = extension }) { versions.CreateNewVersionFile(); } _callUpdate.NeedNotified = true; _callUpdate.Call(subscriber); DirFile.DeleteFile(fileInfoTemp); fileInfoTemp = null; } }); } catch (Exception ex) { Errors.Save(ex); } }
private string GetMD5(string path) { string hash = String.Empty; try { using (MD5 md5 = MD5.Create()) { string fileNameTemp = DirFile.GetTempFile(); FileInfo fileInfoTemp = new FileInfo(fileNameTemp); if (!fileInfoTemp.Exists) { new FileInfo(path).CopyTo(fileNameTemp); _tempFiles.Add(fileInfoTemp); } using (FileStream stream = new FileStream(fileNameTemp, FileMode.Open, FileAccess.Read, FileShare.Read)) { byte[] hashByte = md5.ComputeHash(stream); hash = BitConverter.ToString(hashByte).Replace("-", "").ToLowerInvariant(); stream.Close(); } md5.Clear(); } } catch (FileNotFoundException) { Errors.Save($"Файл '{path}' перемещен или удален."); } catch (IOException ex) { Errors.Save(ex); } return(hash); }