Work with Files and Streams

Use Sharp Runtime I/O with RAII, capability checks and explicit platform error handling.

IntermediateTutorial

Select I/O

set(SHARP_RUNTIME_COMPONENTS IO)
target_link_libraries(app PRIVATE SharpRuntime::IO)

Scope resources

Keep streams and readers/writers in the narrowest practical scope. Destruction is the native cleanup backstop; explicit Dispose-shaped calls may still matter for source-compatible control flow.

Ask before operating

  • Use stream capability properties rather than assuming read/write/seek.
  • Treat short reads and end-of-stream as normal control flow.
  • Distinguish path validation from filesystem failures.
  • Do not hold a callback or span beyond the buffer/resource lifetime it borrows.

Handle platform variation

Paths, watchers, sharing, symlinks and permission errors are platform-dependent. Catch the narrow Sharp Runtime I/O exception only when the application can recover; otherwise preserve the diagnostic.