/// <summary> /// 解压到指定文件夹 /// </summary> /// <param name="zipFileName"></param> /// <param name="targetDirectory"></param> /// <param name="events"></param> public static void ExtractZipThread(string zipFileName, string targetDirectory, FileChangeEvent events) { Thread thread = new Thread(new ThreadStart(() => { ExtractZip(zipFileName, targetDirectory, events); })); thread.Start(); }
public static void NotifyEvent(FileChangeEvent type, FileSystemEventArgs e, FileFilter filter) { FileInfo f = files.FirstOrDefault(x => x.FilePath.Equals(e.FullPath, StringComparison.InvariantCultureIgnoreCase)); if (f == null) { f = new FileInfo(); f.FilePath = e.FullPath; f.Name = e.Name; files.Add(f); } if (File.Exists(e.FullPath)) { System.IO.FileInfo fi = new System.IO.FileInfo(e.FullPath); f.History.Insert(0, new FileStatus() { CreateDate = fi.CreationTime, LastWrite = fi.LastWriteTime, Size = fi.Length, Info = fi }); } List <ActionToDo> actions = new List <ActionToDo>(); //TODO add default action here switch (type) { case FileChangeEvent.Changed: actions.AddRange(filter.OnChange.ToArray()); break; case FileChangeEvent.Created: actions.AddRange(filter.OnNew.ToArray()); break; case FileChangeEvent.Deleted: actions.AddRange(filter.OnDeleted.ToArray()); break; } EventContext context; foreach (var action in actions) { string name = action.ActionName; var p = Configuration.Current.Processors[name]; context = new EventContext(type, f, action.Arguments); p.Process(context); } }
/* * FileChange is called when a file we are monitoring has changed. */ void FileChange(Object sender, FileChangeEvent e) { // Ignore notifications of events occured in the past, before we started monitoring. // This is to avoid the race with a delayed change notification about the file that // the user code just changed before creating the dependency: // 1) User writes to the file // 2) User creates dependency // 3) Change notification comes and needs to be ignored if the timestamp // of the file last access time is earlier than dependency creation time. // // However, if the cache dependency is marked as sensitive, the above check won't be // done. Debug.Trace("CacheDependencyFileChange", "FileChange file=" + e.FileName + ";Action=" + e.Action); if ((_bits & SENSITIVE) == 0 && (e.Action == FileAction.Modified || e.Action == FileAction.Added)) { // only check within some window const int raceWindowSeconds = 5; if (DateTime.UtcNow <= _utcInitTime.AddSeconds(raceWindowSeconds)) { int hr; FileAttributesData fa; hr = FileAttributesData.GetFileAttributes(e.FileName, out fa); if (hr == HResults.S_OK && (fa.FileAttributes & UnsafeNativeMethods.FILE_ATTRIBUTE_DIRECTORY) == 0 && fa.UtcLastAccessTime >= fa.UtcLastWriteTime && // on FAT LastAccessTime is wrong fa.UtcLastAccessTime <= _utcInitTime) { Debug.Trace("CacheDependencyFileChange", "FileChange ignored; fi.LastAccessTime = " + fa.UtcLastAccessTime.ToLocalTime() + "; fi.LastWriteTime = " + fa.UtcLastWriteTime.ToLocalTime() + "; _utcInitTime = " + _utcInitTime.ToLocalTime()); return; // ignore this notification } } } OnChanged(sender, e); }
/// <summary> /// Handle a file change (New,Update,Rename) /// </summary> /// <param name="fe">File Change Event with all the information for the file change</param> public void HandleFileChange(FileChangeEvent fe) { try { switch (fe.Event) { case EventChangeType.CREATED: { HandleFileCreateEvent(fe); } break; case EventChangeType.MODIFIED: { HandleFileModifyEvent(fe); } break; case EventChangeType.RENAMED: { HandleFileRenameEvent(fe); } break; } } catch (Exception e) { ServiceLocator.GetLogger(ServiceLocator.DEBUG_LOG).Write(e); } }
// // FileChange is called when a file we are monitoring has changed. // void FileChange(Object sender, FileChangeEvent e) { Debug.Trace("CacheDependencyFileChange", "FileChange file=" + e.FileName + ";Action=" + e.Action); NotifyDependencyChanged(sender, e); }
private void ExecuteFile(FileSystemEvent fse) { FileChangeEvent fileEvent; switch (fse.EventType) { case EventChangeType.CREATED: //ServiceLocator.GetLogger(ServiceLocator.DEVELOPER_LOG).Write("File Created: " + fse.Path); fileEvent = new FileChangeEvent(new FileInfo(fse.Path), EventChangeType.CREATED); ServiceLocator.MonitorI.HandleFileChange(fileEvent); break; case EventChangeType.MODIFIED: //ServiceLocator.GetLogger(ServiceLocator.DEVELOPER_LOG).Write("File Modified: " + fse.Path); fileEvent = new FileChangeEvent(new FileInfo(fse.Path), EventChangeType.MODIFIED); ServiceLocator.MonitorI.HandleFileChange(fileEvent); break; case EventChangeType.DELETED: //ServiceLocator.GetLogger(ServiceLocator.DEVELOPER_LOG).Write("File Deleted: " + fse.Path); fileEvent = new FileChangeEvent(new FileInfo(fse.Path), EventChangeType.DELETED); ServiceLocator.MonitorI.HandleFileChange(fileEvent); break; case EventChangeType.RENAMED: //ServiceLocator.GetLogger(ServiceLocator.DEVELOPER_LOG).Write("File Renamed: " + fse.OldPath + " " + fse.Path); fileEvent = new FileChangeEvent(new FileInfo(fse.OldPath), new FileInfo(fse.Path)); ServiceLocator.MonitorI.HandleFileChange(fileEvent); break; default: Debug.Assert(false); break; } }
private void OnChanged(object sender, FileChangeEvent e) { this._onChangedCallback(null); }
// // FileChange is called when a file we are monitoring has changed. // void FileChange(Object sender, FileChangeEvent e) { Debug.Trace("CacheDependencyFileChange", "FileChange file=" + e.FileName + ";Action=" + e.Action); NotifyDependencyChanged(sender, e); }
private void Watcher_Changed(object sender, FileSystemEventArgs e) { if (!System.IO.Path.HasExtension(e.FullPath)) { if (this._watcher.IncludeSubdirectories) { switch (e.ChangeType) { case WatcherChangeTypes.Changed: { this._log.Debug($"File {e.FullPath} Trigger Change Event"); FileChangeEvent?.Invoke(e.FullPath); } break; case WatcherChangeTypes.Created: { this._log.Debug($"File {e.FullPath} Trigger Create Event"); FileCreateEvent?.Invoke(e.FullPath); } break; case WatcherChangeTypes.Deleted: { this._log.Debug($"File {e.FullPath} Trigger Remove Event"); FileRemoveEvent?.Invoke(e.FullPath); } break; } } else { this._log.Debug($"not exist {e.FullPath} file"); } return; } switch (e.ChangeType) { case WatcherChangeTypes.Changed: { DateTime prevDate; if (_fileChangeDate.TryGetValue(e.FullPath, out prevDate)) { var currentDate = File.GetLastWriteTime(e.FullPath); if (prevDate == currentDate) { return; } _fileChangeDate[e.FullPath] = currentDate; } else { _fileChangeDate[e.FullPath] = DateTime.Now; } this._log.Debug($"File {e.FullPath} Trigger Change Event"); FileChangeEvent?.Invoke(e.FullPath); } break; case WatcherChangeTypes.Created: { this._log.Debug($"File {e.FullPath} Trigger Create Event"); FileCreateEvent?.Invoke(e.FullPath); } break; case WatcherChangeTypes.Deleted: { this._log.Debug($"File {e.FullPath} Trigger Remove Event"); FileRemoveEvent?.Invoke(e.FullPath); } break; } }
internal void OnConfigChange(Object sender, FileChangeEvent e) { HttpRuntime.OnConfigChange(sender, e); }
public EventContext(FileChangeEvent eventToHandle, FileInfo file, Dictionary <string, string> Arguments) { this.Event = eventToHandle; this.File = file; this.Arguments = Arguments; }
private void FileChange(object sender, FileChangeEvent e) { this.NotifyDependencyChanged(sender, e); }
/// <summary> /// 多线程指定添加文件 /// </summary> /// <param name="sourceFilesPath"></param> /// <param name="destinationZipFilePath"></param> /// <param name="events"></param> /// <param name="recurse"></param> /// <param name="fileFilter"></param> public static void CreateZipThread(Dictionary <string, string> sourceFilesPath, string destinationZipFilePath, FileChangeEvent events) { Thread thread = new Thread(new ThreadStart(() => { CreateZip(sourceFilesPath, destinationZipFilePath, events); })); thread.Start(); }
/// <summary> /// 将指定文件压缩到zip文件 /// </summary> /// <param name="sourceFilesPath"></param> /// <param name="destinationZipFilePath"></param> /// <param name="events"></param> /// <param name="recurse"></param> /// <param name="fileFilter"></param> public static void CreateZip(Dictionary <string, string> sourceFilesPath, string destinationZipFilePath, FileChangeEvent events) { ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath)); zipStream.SetLevel(9); // 压缩级别 0-9 Crc32 crc = new Crc32(); Dictionary <ZipEntry, byte[]> holders = new Dictionary <ZipEntry, byte[]>(); long total = 0; long current = 0; foreach (var file in sourceFilesPath) { if (File.Exists(file.Key)) { FileStream fileStream = File.OpenRead(file.Key); byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(file.Value); entry.DateTime = DateTime.Now; entry.Size = fileStream.Length; entry.IsUnicodeText = true; fileStream.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; holders.Add(entry, buffer); total += buffer.Length; } } foreach (var hoder in holders) { zipStream.PutNextEntry(hoder.Key); zipStream.Write(hoder.Value, 0, hoder.Value.Length); if (events != null) { events.Invoke(hoder.Key.Name, total, current += hoder.Value.Length); } } zipStream.Finish(); zipStream.Close(); }
public static void ExtractZip(string zipFileName, string targetDirectory, FileChangeEvent events) { if (!File.Exists(zipFileName)) { return; } if (!Directory.Exists(targetDirectory)) { Directory.CreateDirectory(targetDirectory); } ZipInputStream s = null; ZipEntry theEntry = null; string fileName; FileStream streamWriter = null; try { long total = 0; long current = 0; s = new ZipInputStream(File.OpenRead(zipFileName)); Dictionary <string, byte[]> dataDic = new Dictionary <string, byte[]>(); while ((theEntry = s.GetNextEntry()) != null) { total += s.Length; fileName = Path.Combine(targetDirectory, theEntry.Name); if (fileName.EndsWith("/") || fileName.EndsWith("\\")) { Directory.CreateDirectory(fileName); continue;//文件夹跳过 } if (theEntry.Name != String.Empty) { using (var mem = new MemoryStream()) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { mem.Write(data, 0, size); } else { break; } } dataDic.Add(theEntry.Name, mem.ToArray()); } } } foreach (var item in dataDic) { fileName = Path.Combine(targetDirectory, item.Key); ///判断文件路径是否是文件夹 if (!Directory.Exists(Path.GetDirectoryName(fileName))) { Directory.CreateDirectory(Path.GetDirectoryName(fileName)); } current += item.Value.Length; streamWriter = File.Create(fileName); streamWriter.Write(item.Value, 0, item.Value.Length); if (events != null) { events.Invoke(item.Key, total, current); } } } catch { throw; } finally { if (streamWriter != null) { streamWriter.Close(); streamWriter = null; } if (theEntry != null) { theEntry = null; } if (s != null) { s.Close(); s = null; } GC.Collect(); GC.Collect(1); } }
internal void OnConfigFileChanged(Object sender, FileChangeEvent e) { string message = FileChangesMonitor.GenerateErrorMessage(e.Action, e.FileName); HttpRuntime.OnConfigChange(message); }
internal void OnFileChanged(object sender, FileChangeEvent e) { _callback(e.FileName); }
private void HandleFileCreateEvent(FileChangeEvent fe) { //Find the logical Address for the old path //Do not create the logical address if not found. string logicalAddress = ProfilingLayer.Instance.ConvertPhysicalToLogical(fe.OldPath.FullName, false); //Get all the tag that contains this logicalAddress inside the tag. List<Tag> tag = TaggingLayer.Instance.RetrieveParentTagByPath(logicalAddress); //If tag count is 0 return as there is nothing to process. if (tag.Count == 0) // { return; } //Find the similiar paths for the logical Path //The return is physical address List<string> convertedList = FindSimilarSeamlessPathForFile(logicalAddress); convertedList.Remove(fe.OldPath.FullName); //////// Massive Path Table Code ///////////// for (int i = 0; i < convertedList.Count; i++) { string dest = convertedList[i]; if (_pathTable.JustPop(fe.OldPath.FullName, dest, TableType.Create)) { convertedList.Remove(dest); i--; continue; } } ///////////////// For each Path in the converted List /////////////////// ///////////////////////////////// Create a entry such that source = convertedPath and destination is the original Path /////////// ///////////////////////For each other path , create a Source + dest pair ////////// ///////////////// Create an additional Path Entry for each of the Siblings //////////////// for (int i = 0; i < convertedList.Count; i++) { _pathTable.AddPathPair(convertedList[i], fe.OldPath.FullName, TableType.Create); for (int j = i + 1; j < convertedList.Count; j++) { _pathTable.AddPathPair(convertedList[i], convertedList[j], TableType.Create); _pathTable.AddPathPair(convertedList[j], convertedList[i], TableType.Create); } } ///////// End of Path Table Code //////////////// //For each path in the convertedList , extract the parent Path. List<string> parentList = new List<string>(); foreach (string path in convertedList) { FileInfo info = new FileInfo(PathHelper.RemoveTrailingSlash(path)); string parent = info.Directory == null ? "" : info.Directory.FullName; parentList.Add(parent); } //If the parent list if empty , it means that there is nothing to sync and thus return. if (parentList.Count == 0) { return; } //Create the request and Send it. if (fe.OldPath.Directory != null) { AutoSyncRequest request = new AutoSyncRequest(fe.OldPath.Name, fe.OldPath.Directory.FullName, parentList, false, AutoSyncRequestType.New, SyncConfig.Instance, ConvertTagListToTagString(tag)); SendAutoRequest(request); } }
private void OnWebHashFileChange(Object sender, FileChangeEvent e) { // Shutdown the app domain Debug.Trace("BuildManager", _webHashFilePath + " changed - shutting down the app domain"); Debug.Trace("AppDomainFactory", "Shutting down appdomain because " + _webHashFilePath + " file changed"); string message = FileChangesMonitor.GenerateErrorMessage(e.Action, _webHashFilePath); if (message == null) { message = "Change in " + _webHashFilePath; } HttpRuntime.ShutdownAppDomain(ApplicationShutdownReason.BuildManagerChange, message); }
internal void OnConfigFileChanged(object sender, FileChangeEvent e) { HttpRuntime.OnConfigChange(); }
private void OnChanged(Object sender, FileChangeEvent e) { _onChangedCallback(null); }