liveBook · Manning

Directory separator

As explained in ASP.NET Core in Action, 3e

A path directory separator is the platform-specific character that divides directory and file names in a filesystem path.

A directory separator divides directory and file names in a path. Windows uses a backslash (\), while Linux uses a forward slash (/). Hardcoded Windows paths can therefore fail after deployment to Linux. section 27.3.2

Build a portable path

Instead of writing "subdir\\myfile.json", construct the path with Path.Combine("subdir", "myfile.json"). This selects the appropriate separator for the platform, allowing the same application to load the file on Windows and Linux. section 27.3.2

When deploying across operating systems:

  • Avoid hardcoding directory separators in application paths. section 27.3.2
  • Use Path.Combine to create paths for the current platform. section 27.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains separator differences and the Path.Combine remedy, without detailing broader filesystem APIs.
Examples
Uses the contrasting paths "subdir\\myfile.json" and Path.Combine("subdir", "myfile.json").
What you can do after
Apply it via Path.Combine to construct file paths that work on the current operating system.
How it connects
Connects directory separators with Windows, Linux, filesystem paths, deployment, and Path.Combine.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.