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.
file:
?)-URI compliant - it's a UNC path, which is not a URI.