lonix
lonix

Reputation: 20777

Expand paths with tilde (~) in .NET Core

If I use a disk (not URL) path with ~, e.g. ~/mike/foo, then the runtime appends it to the current working directory instead of expanding it.

These don't work:

Is there a built-in .NET Core function that would expand such a path safely, cross-platform?

Upvotes: 10

Views: 1495

Answers (1)

lonix
lonix

Reputation: 20777

My cross-platform workaround:

myPath = myPath
  .Replace("~", Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
  .Replace("//", "/");

But there are presumably edge cases (especially cross-platform), as always. So a built-in .NET Core function, if one exists, is preferable.

(Please add your answer and I'll accept it.)

Upvotes: 10

Related Questions