Skip to content

Observed Text File

The ObservedTextFile class in the ByteCobra.Observables namespace represents an observable text file, allowing subscribers to be notified of file changes. This class relies on the FileSystemWatcher to monitor the specified file for changes, deletions, creations, and renaming events, and notifies subscribed clients accordingly.

The ObservedTextFile class encapsulates a FileSystemWatcher instance to monitor the specified text file for any changes, deletions, creations, or renaming events. When any of these events occur, the corresponding event handler (OnTextFileChanged, OnTextFileDeleted, or OnTextFileRenamed) is invoked. These handlers then call the NotifySubscribers method to notify all subscribed clients about the change, passing along the new file contents and the type of change.

The Subscribe, Unsubscribe, and ClearSubscriptions methods manage a list of subscribers, which are client callback methods to be invoked when the observed text file changes. The ReadAllText, WriteAllLines, WriteAllText, AppendText, and the overloaded AppendText methods provide read and write access to the file contents, with the write and append methods also triggering change notifications to subscribed clients.

This class is designed to provide a robust mechanism for monitoring text file changes in a decoupled and type-safe manner, facilitating a variety of scenarios where clients need to react to changes in file data.

Properties

  • string Path: Gets the path of the observed text file.
  • bool HandleExceptions: Gets or sets whether exceptions should be handled when notifying subscribers.

Methods

  • ObservedTextFile(string path): Constructor that initializes a new instance of the ObservedTextFile class with the specified file path.
  • void Subscribe(Action<string, WatcherChangeTypes> callback): Subscribes a callback method to be called when the text file changes.
  • void Unsubscribe(Action<string, WatcherChangeTypes> callback): Unsubscribes a callback method from receiving text file change notifications.
  • void ClearSubscriptions(): Clears all subscriptions to this observed text file.
  • string ReadAllText(): Reads all text from the observed text file.
  • void Move(string path): Moves the observed text file to the specified path and notifies subscribers of the change.
  • void WriteAllLines(string[] lines): Writes an array of lines to the observed text file and notifies subscribers of the change.
  • void WriteAllText(string text): Writes text to the observed text file and notifies subscribers of the change.
  • void AppendText(string text): Appends text to the end of the observed text file and notifies subscribers of the change.
  • void AppendText(string text, Encoding encoding): Appends text to the end of the observed text file with the specified encoding and notifies subscribers of the change.

Event Handling Methods

  • protected virtual void OnTextFileDeleted(object sender, FileSystemEventArgs e): Handles the file deleted event.
  • protected virtual void OnTextFileRenamed(object sender, RenamedEventArgs e): Handles the file renamed event.
  • protected virtual void OnTextFileChanged(object sender, FileSystemEventArgs e): Handles the file changed event.
  • protected virtual void NotifySubscribers(string text, WatcherChangeTypes type): Notifies all subscribers about a change in the observed text file.

Example

var observedFile = new ObservedTextFile("example.txt");

observedFile.Subscribe((text, changeType) =>
{
    Console.WriteLine($"File changed with type: {changeType}");
});

observedFile.WriteAllText("Hello, World!");
// Output: File changed with type: Created

observedFile.AppendText("\nAppended Text.");
// Output: File changed with type: Changed