4 ms·
Coding with file system paths means accepting just a few difficult facts. - a path is not a unique identifier of a file system object. They don’t have canonica
by alkonaut 3y ago
Coding with file system paths means accepting just a few difficult facts.
- a path is not a unique identifier of a file system object. They don’t have canonical names.
- don’t make any assumptions about case sensitivity, what file names are valid, or what the longest possible path is.
- avoid baking paths from strings as as much as possible. Use constructs like PathBuf (Rust) that prevent footguns like duplicated or incorrect dir separators. Never use a literal path separator char ‘/‘ or ‘\’ in code is a good rule of thumb.
- agumonkey 3y agoI remember one article about someone porting code from *nix to windows and having almost no issue due to the author leveraging pathlib. Just a side note.
- Const-me 3y agoSame applies to C#. The standard library has pretty good APIs for dealing with file names and paths. In my experience, a program written in modern .NET 5+ and tested on Windows has a high probability to run flawlessly on Linux, without even a recompilation.