1

我想将以下 PATH 转换为 Ruby 中的 UNC 路径。

C:/Users/bla/bla2/asdf-ut-script.js

4

1 回答 1

1

UNC 路径要求您知道服务器和共享的名称,它们都不存在于您的路径中,除非您正在寻找类似的内容:
\\localhost\C$\Users\bla\bla2\asdf-ut-script.js

如果这是你想要的:

def File.to_unc( path, server="localhost", share=nil )
  parts = path.split(File::SEPARATOR)
  parts.shift while parts.first.empty?
  if share
    parts.unshift share
  else
    # Assumes the drive will always be a single letter up front
    parts[0] = "#{parts[0][0,1]}$" 
  end
  parts.unshift server
  "\\\\#{parts.join('\\')}"
end

puts File.to_unc( "C:/Users/bla/bla2/asdf-ut-script.js" )
#=> \\localhost\C$\Users\bla\bla2\asdf-ut-script.js

puts File.to_unc( "C:/Users/bla/bla2/asdf-ut-script.js", 'filepile' )
#=> \\filepile\C$\Users\bla\bla2\asdf-ut-script.js

puts File.to_unc( "/bla/bla2/asdf-ut-script.js", 'filepile', 'HOME' )
#=> \\filepile\HOME\bla\bla2\asdf-ut-script.js
于 2011-02-01T14:44:34.463 回答