void IFileChangeNotificationSystem.StartMonitoring(string filePath, OnChangedCallback onChangedCallback, out Object state, out DateTimeOffset lastWrite, out long fileSize) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (onChangedCallback == null) { throw new ArgumentNullException("onChangedCallback"); } FileChangeEventTarget target = new FileChangeEventTarget(onChangedCallback); FileAttributesData fad; HttpRuntime.FileChangesMonitor.StartMonitoringPath(filePath, target.Handler, out fad); if (fad == null) { fad = FileAttributesData.NonExistantAttributesData; } state = target; #if DBG Debug.Assert(fad.UtcLastWriteTime.Kind == DateTimeKind.Utc, "fad.UtcLastWriteTime.Kind == DateTimeKind.Utc"); #endif lastWrite = fad.UtcLastWriteTime; fileSize = fad.FileSize; }
void IFileChangeNotificationSystem.StopMonitoring(string filePath, Object state) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (state == null) { throw new ArgumentNullException("state"); } FileChangeEventTarget target = state as FileChangeEventTarget; if (target == null) { throw new ArgumentException(R.Invalid_state, "state"); } string dir = Path.GetDirectoryName(filePath); DirectoryMonitor dirMon = _dirMonitors[dir] as DirectoryMonitor; if (dirMon != null) { lock (dirMon) { dirMon.Fsw.Changed -= target.ChangedHandler; dirMon.Fsw.Created -= target.ChangedHandler; dirMon.Fsw.Deleted -= target.ChangedHandler; dirMon.Fsw.Error -= target.ErrorHandler; dirMon.Fsw.Renamed -= target.RenamedHandler; } } }
void IFileChangeNotificationSystem.StartMonitoring(string filePath, OnChangedCallback onChangedCallback, out object state, out DateTimeOffset lastWriteTime, out long fileSize) { if (filePath is null) { throw new ArgumentNullException(nameof(filePath)); } if (onChangedCallback is null) { throw new ArgumentNullException(nameof(onChangedCallback)); } FileInfo fileInfo = new FileInfo(filePath); string dir = Path.GetDirectoryName(filePath); DirectoryMonitor dirMon = _dirMonitors[dir] as DirectoryMonitor; if (dirMon == null) { lock (_lock) { dirMon = _dirMonitors[dir] as DirectoryMonitor; if (dirMon == null) { dirMon = new DirectoryMonitor(); dirMon.Fsw = new FileSystemWatcher(dir); dirMon.Fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.CreationTime | NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.Security; dirMon.Fsw.EnableRaisingEvents = true; } _dirMonitors[dir] = dirMon; } } FileChangeEventTarget target = new FileChangeEventTarget(fileInfo.Name, onChangedCallback); lock (dirMon) { dirMon.Fsw.Changed += target.ChangedHandler; dirMon.Fsw.Created += target.ChangedHandler; dirMon.Fsw.Deleted += target.ChangedHandler; dirMon.Fsw.Error += target.ErrorHandler; dirMon.Fsw.Renamed += target.RenamedHandler; } state = target; lastWriteTime = File.GetLastWriteTime(filePath); fileSize = (fileInfo.Exists) ? fileInfo.Length : -1; }
void IFileChangeNotificationSystem.StartMonitoring(string filePath, OnChangedCallback onChangedCallback, out object state, out DateTimeOffset lastWriteTime, out long fileSize) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (onChangedCallback == null) { throw new ArgumentNullException("onChangedCallback"); } FileInfo info = new FileInfo(filePath); string directoryName = Path.GetDirectoryName(filePath); DirectoryMonitor monitor = this._dirMonitors[directoryName] as DirectoryMonitor; if (monitor == null) { lock (this._lock) { monitor = this._dirMonitors[directoryName] as DirectoryMonitor; if (monitor == null) { monitor = new DirectoryMonitor { Fsw = new FileSystemWatcher(directoryName) }; monitor.Fsw.NotifyFilter = NotifyFilters.Security | NotifyFilters.CreationTime | NotifyFilters.LastWrite | NotifyFilters.Size | NotifyFilters.DirectoryName | NotifyFilters.FileName; monitor.Fsw.EnableRaisingEvents = true; } this._dirMonitors[directoryName] = monitor; } } FileChangeEventTarget target = new FileChangeEventTarget(info.Name, onChangedCallback); lock (monitor) { monitor.Fsw.Changed += target.ChangedHandler; monitor.Fsw.Created += target.ChangedHandler; monitor.Fsw.Deleted += target.ChangedHandler; monitor.Fsw.Error += target.ErrorHandler; monitor.Fsw.Renamed += target.RenamedHandler; } state = target; lastWriteTime = File.GetLastWriteTime(filePath); fileSize = info.Exists ? info.Length : -1L; }
void IFileChangeNotificationSystem.StartMonitoring(string filePath, OnChangedCallback onChangedCallback, out object state, out DateTimeOffset lastWrite, out long fileSize) { FileAttributesData nonExistantAttributesData; if (filePath == null) { throw new ArgumentNullException("filePath"); } if (onChangedCallback == null) { throw new ArgumentNullException("onChangedCallback"); } FileChangeEventTarget target = new FileChangeEventTarget(onChangedCallback); HttpRuntime.FileChangesMonitor.StartMonitoringPath(filePath, target.Handler, out nonExistantAttributesData); if (nonExistantAttributesData == null) { nonExistantAttributesData = FileAttributesData.NonExistantAttributesData; } state = target; lastWrite = nonExistantAttributesData.UtcLastWriteTime; fileSize = nonExistantAttributesData.FileSize; }
void IFileChangeNotificationSystem.StartMonitoring(string filePath, OnChangedCallback onChangedCallback, out Object state, out DateTimeOffset lastWriteTime, out long fileSize) { if (filePath == null) { throw new ArgumentNullException("filePath"); } if (onChangedCallback == null) { throw new ArgumentNullException("onChangedCallback"); } FileInfo fileInfo = new FileInfo(filePath); string dir = Path.GetDirectoryName(filePath); DirectoryMonitor dirMon = _dirMonitors[dir] as DirectoryMonitor; if (dirMon == null) { lock (_lock) { dirMon = _dirMonitors[dir] as DirectoryMonitor; if (dirMon == null) { dirMon = new DirectoryMonitor(); dirMon.Fsw = new FileSystemWatcher(dir); dirMon.Fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.CreationTime | NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.Security; dirMon.Fsw.EnableRaisingEvents = true; } _dirMonitors[dir] = dirMon; } } FileChangeEventTarget target = new FileChangeEventTarget(fileInfo.Name, onChangedCallback); lock (dirMon) { dirMon.Fsw.Changed += target.ChangedHandler; dirMon.Fsw.Created += target.ChangedHandler; dirMon.Fsw.Deleted += target.ChangedHandler; dirMon.Fsw.Error += target.ErrorHandler; dirMon.Fsw.Renamed += target.RenamedHandler; } state = target; lastWriteTime = File.GetLastWriteTime(filePath); fileSize = (fileInfo.Exists) ? fileInfo.Length : -1; }