I ran into this same issue. In my case I was writing to a local mapped directory on Server 2016. When I ran in debug and hit the .Dispose() or .Close() methods on the writer it would delete the file from the output location.
Running the application outside of Visual Studio (2019, 2022) in release the problem didn't persist. Seems in my case it was the development environment that caused the issue.