Ruby 中是否有内置方法可以转换 Windows 短路径,例如
C:\PROGRA~2\MICROS~1.0\VC\bin\amd64
进入其相应的长路径
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64
我已经尝试过
File.expand_path(short_path)
路径名.new(short_path).cleanpath
路径名.new(short_path).realpath
Dir.chdir(short_path) { Dir.pwd }
但这些都不起作用。
如果可能的话,我想避免像 this answer 那样直接调用 Win32 API ,或者像生成 PowerShell 这样丑陋的解决方法:
%x{powershell (Get-Item -LiteralPath #{short_path}).FullName}
最佳答案
这是一种可能的解决方法:
path=Dir.mktmpdir('vendor')
=> "C:/Users/ADMINI~1/AppData/Local/Temp/1/vendor20160727-12668-ywfjol"
Dir.glob(path)[0]
=> "C:/Users/Administrator/AppData/Local/Temp/1/vendor20160727-12668-ywfjol"
关于将Windows短路径转换为长路径的Ruby方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34919422/