1

What's the .NET or Windows API to perform conversion of \\wsl$\ into URI-compliant \\wsl.localhost\?

Windows Explorer converts paths inlcuding \\wsl$\ to \\wsl.localhost\ and I believe that this is related to wsl2. I would like to use supported API to avoid hardcoding the conversion from wsl$ to wsl.localhost and potentially missing any edge cases.

4
  • 1
    Are you running Windows 11 21H2? \\wsl.localhost\\ is the path on Windows 11 otherwise if you are running Windows 10 21H2 it’s \\wsl\\ if you provide the necessary details I will submit an answer. What do either of those paths have to do with .NET?
    – Ramhound
    Commented Jun 13, 2022 at 23:10
  • I'm running Windows 11 21H2 but my customers may be running other versions of Windows. The only reason I mentioned .NET is I thought there might be some method (e.g. in System.IO.Path) which would sort this out. Commented Jun 14, 2022 at 16:11
  • You failed to provide that vital information in your question. It's not clear how you are using the path to WSL2, but you will have to perform conditional checks, on if a PATH exists to support both Windows 10 21H2 and Windows 11 21H2. If it's .NET code you can just check if the PATH exists.
    – Ramhound
    Commented Jun 14, 2022 at 16:40
  • @AmadeuszWieczorek, <code>\\wsl.localhost\</code> isn't (file:?)-URI compliant - it's a UNC path, which is not a URI. Commented 3 hours ago

1 Answer 1

2

As far as I know, there's no .NET or Windows API that is going to provide this to you since it's not Windows that sets up the network share -- It's, of course, WSL itself.

On Windows 10, as @Ramhound mentioned in the comments, WSL sets up a network share called \\wsl$\<distroname> for each running distro.

On Windows 11, it actually registers both \\wsl$\<distroname> and \\wsl.localhost\<distroname>. It actually does this for all installed WSL2 distributions, not just those that are running. However, the "default" is now the \\wsl.localhost\<distroname> share.

I did come up with a bit of a hacky workaround, though.

Since WSL does have knowledge of this share (and the default), we can utilize the wslpath command through WSL to obtain the proper path. Round-tripping a \\wsl$\ path through wslpath will return the proper "default" path for either Windows 10 or 11.

For instance, from inside WSL (Bash):

p='\\wsl$\Ubuntu\home'
wslpath -w $(wslpath -u $p)
  • Returns \\wsl.localhost\Ubuntu\home on Windows 11
  • Returns \\wsl$\Ubuntu\home (no change) on Windows 10

From PowerShell, you can call it via the wsl.exe command itself, although quoting/escaping starts to get more complicated:

$p = '\\\\wsl$\\Ubuntu\\home'
wsl ~ -e bash -c "wslpath -w `$(wslpath -u $p)"

Which returns the same results as above.

There's one huge caveat here, though -- This works if you only have one distribution. If you have multiple distributions, then a path in any given distribution is only known by that particular distribution.

In other words, if you had distributions named "Ubuntu" and "Debian", then the above would only work if Ubuntu was your default distribution. If you tried to normalize a path in the Debian distribution (e.g. \\wsl$\Debian\home) then you would need to do that through the Debian distribution. Such as (from PowerShell):

$p = '\\\\wsl$\\Debian\\home'
wsl ~ -d Debian -e bash -c "wslpath -w `$(wslpath -u $p)"

That certainly complicates matters quite a bit more.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .