Chapter 10
Files, Folders and FTP
Business software moves files around far more than its architecture diagrams admit. A bank drops a reconciliation file on an SFTP server at 4 A.M. A plant uploads a spreadsheet. A nightly job writes an extract that another job, owned by another team, picks up an hour later. When any of those steps fails halfway, someone spends a morning working out which files were complete.
File handling turned up in 27 of the engagements in the archive behind this book, with 5,843 file- and FTP-related classes. FileManager is one of the most copied helper class names, at 267 copies. The same problems recur in them: paths built by string concatenation, files read by another process while they were still being written, a FileSystemWatcher that fired three times for one file, FTP code that trusted any server it connected to, and temp folders that grew until the disk filled.
This chapter gives you seven small tools that build on each other. The safe path helper from the first recipe shows up again in the zip and SFTP recipes, and the atomic writer is used by the SFTP downloader.
Recipes
- 10.1Safe Path HandlingCode combines a base folder with a name that came from outside: a query string, an uploaded file name, a zip entry, a column in an import file. If…
- 10.2Atomic File WritesA job writes an output file. Another process, or a person, reads it while it is half written and gets a truncated file. Or the job crashes in the…
- 10.3FileSystemWatcher Done RightA service needs to process files as they arrive in a folder. FileSystemWatcher looks like the answer, and then the bugs start. It raises Created and…
- 10.4Zip and Unzip Without SurprisesZip files arrive from partners and users, and go out as bundles of reports. Creating them is easy. Extracting them is where the risk is. An entry…
- 10.5SFTP with SSH.NETPartners exchange files over SFTP. The integration code usually connects with a password stored in configuration, accepts whatever host key the…
- 10.6Streaming Large FilesCode that worked with test files falls over with production ones. File.ReadAllBytes on a 3 GB extract throws OutOfMemoryException, or pushes a web…
- 10.7Temp Files That Clean Up After ThemselvesCode creates temporary files for conversions, downloads and report generation, and forgets them. On one manufacturing client's report server, the…
Chapter checklist
- Route every path built from outside input through
SafePath.Combine, and return 404 when it fails. - Write files that other processes read with
AtomicFile: temp file in the same folder, flush, rename. - Treat
FileSystemWatcheras a hint: queue paths, wait for exclusive access, and rescan at startup and onError. - Extract outside archives with path checks plus limits on entries, total bytes and compression ratio.
- Pin SFTP host keys by SHA-256 fingerprint, prefer key authentication, and upload to
.partthen rename. - Stream large files in chunks and hash them in the same pass with SHA-256.
- Replace
Path.GetTempFileNamewith disposable temp files under one app folder, and sweep it at startup.