FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
C#File operations & paths

Copy a file without overwriting

C#: File.Copy defaults to refusing a destination that already exists.

C#✓ Tested
C#File operations & paths

Create a file only when it does not exist

C#: FileMode.CreateNew prevents accidental replacement of existing content.

C#✓ Tested
C#File operations & paths

Delete a known scratch file

C#: File.Delete ignores a missing file but still throws for other I/O failures.

C#✓ Tested
C#File operations & paths

Enumerate matching files recursively

C#: Sort relative filenames when reproducible traversal output is required.

C#✓ Tested
C#File operations & paths

Inspect a file length without reading it

C#: Use FileInfo.Length to retrieve a logical size in bytes.

C#✓ Tested
C#File operations & paths

Move a file to a new filename

C#: File.Move changes the file path without loading its contents.

C#✓ Tested
C#File operations & paths

Process a file as a lazy line sequence

C#: ReadLines avoids allocating one string containing the entire file.

C#✓ Tested
C#File operations & paths

Read a filename stem and extension

C#: Use Path helpers instead of splitting a path on dots or directory separators.

C#✓ Tested
C#File operations & paths

Replace a path extension

C#: Path.ChangeExtension transforms a path string without moving or creating a file.

C#✓ Tested
C#File operations & paths

Write and read raw bytes

C#: Use the byte-oriented file APIs when no text encoding should be applied.

C#✓ Tested