2011-02-01 186 views

回答

1

一個UNC path要求您知道服務器和共享,這兩者都不存在於你的路徑名,除非你正在尋找的東西,如:
\\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 
+0

謝謝s m8非常感謝! – wmitchell 2011-02-03 12:45:26