Skip to content

FileSystemWatcher in C#: retarget net10.0, drop the broken dispose pattern, IOException - #2233

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/74411-csharp-filesystemwatcher
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/74411-csharp-filesystemwatcher

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Sample update for the article "FileSystemWatcher in C#: How to Monitor File Changes" (https://code-maze.com/csharp-filesystemwatcher/). Touches only files-csharp/FileSystemWatcherExample.

Changes:

  • Both projects retargeted from net6.0 to net10.0.
  • TextFileManager: the finalizer and the public Dispose(bool) overload are removed. Dispose() called GC.SuppressFinalize(true), which boxes a bool and suppresses nothing, and the public Dispose(bool) let any caller mark the instance disposed without disposing the watcher. The class owns one managed resource, so Dispose() now disposes the FileSystemWatcher directly. The misspelled _desposed field went with the pattern.
  • TextFileManager.Create() throws IOException instead of the base Exception type, same message.
  • FileUtil: misspelled field _dircetoryToMonitor renamed to _directoryToMonitor.
  • Program.cs: one comment above the four file operations saying the output order is not guaranteed, because the watcher raises its events on another thread. No behaviour change.

Packages, re-queried from api.nuget.org on 2026-09-23 (newest stable):

  • Microsoft.NET.Test.Sdk 17.1.0 -> 18.10.1 (the batch plan said 18.10.0; the re-query wins)
  • xunit 2.4.1 -> 2.9.3
  • xunit.runner.visualstudio 2.4.3 -> 4.0.0
  • coverlet.collector 3.1.2 -> 10.0.1

Verified locally on SDK 10.0.302, runtime .NET 10.0.10: dotnet build -c Release 0 warnings, 0 errors; dotnet test 4 passed, 0 failed; dotnet list package --vulnerable --include-transitive reports no vulnerable packages on either project.

…ttern, IOException

TextFileManager.Dispose() called GC.SuppressFinalize(true), which boxes a bool and
suppresses nothing, and Dispose(bool) was public, so any caller could mark the
instance disposed without disposing the watcher. The class owns one managed
resource, so the finalizer and the Dispose(bool) overload are removed and
Dispose() disposes the FileSystemWatcher directly.

Also:
- both projects retargeted from net6.0 to net10.0
- Create() throws IOException instead of the base Exception type
- misspelled field FileUtil._dircetoryToMonitor renamed to _directoryToMonitor
  (TextFileManager._desposed went away with the dispose pattern)
- Program.cs notes above the four file operations that the output order is not
  guaranteed, because the watcher raises its events on another thread
- Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0,
  coverlet.collector 10.0.1

Build is clean and tests run 4 of 4 on SDK 10.0.302.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant