Windowsのパス表現(例: C:\data\ )とWSLのパス表現(例: /mnt/c/data )を変換するための方法として、WSL側にはwslpathコマンドがサポートされていますがWindows側 .NETのライブラリでは簡単に変換できる方法がないようです。
WSL環境のwslpathコマンドを呼び出しパス表現の変換を行うための関数を作成しました。
' This software is distributed under the license of NYSL. ' ( http://www.kmonos.net/nysl/ ) ''' <summary> ''' Windowsのパス表現からWSLのパス表現に変換する ''' </summary> ''' <param name="distro">ディストリビューションの起動コマンド(ubuntu、ubuntu1804など)</param> ''' <param name="windowsPath">Windowsのパス</param> ''' <returns>WSLのパス</returns> Public Shared Function ToWslPath(distro As String, windowsPath As String) As String Dim psi As New ProcessStartInfo() psi.FileName = distro psi.Arguments = "run ""wslpath '" & windowsPath.Replace("\", "/") & "'""" psi.RedirectStandardOutput = True psi.UseShellExecute = False psi.CreateNoWindow = True Dim p = Process.Start(psi) Dim wslPath = p.StandardOutput.ReadLine p.WaitForExit() Return wslPath End Function ''' <summary> ''' WSLのパス表現からWindowsのパス表現に変換する ''' </summary> ''' <param name="distro">ディストリビューションの起動コマンド(ubuntu、ubuntu1804など)</param> ''' <param name="wslPath">WSLのパス</param> ''' <param name="slash">Trueの場合セパレータが"/"、Falseの場合セパレータが"\"</param> ''' <returns>Windowsのパス</returns> Public Shared Function ToWindowsPath(distro As String, wslPath As String, Optional slash As Boolean = False) As String Dim psi As New ProcessStartInfo() psi.FileName = distro psi.Arguments = "run ""wslpath " & If(slash, "-m", "-w") & " '" & wslPath & "'""" psi.RedirectStandardOutput = True psi.UseShellExecute = False psi.CreateNoWindow = False Dim p = Process.Start(psi) Dim windowsPath = p.StandardOutput.ReadLine p.WaitForExit() Return windowsPath End Function
Please give us your valuable comment