如何在 Windows 上的 Ruby 中寻址 UNC 路径?
我正在尝试通过 Windows 上的 irb 访问 UNC 共享。在 Windows shell 中,
\\server\share
我尝试转义所有反斜杠。
irb(main):016:0> Dir.entries '\\\\server\share'
Errno::ENOENT: No such file or directory - \\server\share
并使用 IP 地址代替名称
irb(main):017:0> Dir.entries '\\\\192.168.10.1\share'
Errno::ENOENT: No such file or directory - \\192.168.10.1\share
I'm trying to access a UNC share via irb
on Windows. In the Windows shell it would be
\\server\share
I tried escaping all of the backslashes.
irb(main):016:0> Dir.entries '\\\\server\share'
Errno::ENOENT: No such file or directory - \\server\share
and using the IP address instead of the name
irb(main):017:0> Dir.entries '\\\\192.168.10.1\share'
Errno::ENOENT: No such file or directory - \\192.168.10.1\share
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试用另一个 '\' 转义 '\'
Try to escape '\' with another '\'
Ruby 以 POSIX 方式 解释路径,这意味着您应该尽可能使用正斜杠。
尾部斜杠是不必要的,就像在本机 Windows 中一样。您可以使用反斜杠,但必须使用另一个反斜杠转义。
我只建议当您直接从本机程序传递 UNC 路径并且无法转换它们时。当我混合 Ruby/Windows 路径时,例如在使用 Ruby 方法和本机 Windows 应用程序的构建脚本中,每个路径都需要不同的路径,我将使用一些帮助器:
如果路径是文字,则始终将路径用单引号引起来,或者如果要插值则用双引号。正斜杠告诉 Ruby 开始解释正则表达式。这是我在 irb 中常见的错误。
Ruby interprets paths in a POSIX way, meaning you should use forward slashes when possible.
The trailing slash is unnecessary, just like in native Windows. You can use backslashes, but they have to be escaped with another backslash.
I'd only recommend that when you're passing UNC paths from native programs directly and can't transform them. When I'm mixing Ruby/Windows paths, like in a build script that uses Ruby methods and native Windows apps, which each require different paths, I'll use some helpers:
Always enclose your paths in single quotes, if they're literal, or double-quotes if you're interpolating. Forward slashes tell Ruby to start interpreting a regex. This is a common error for me in irb.
看起来您缺少尾部斜杠。尝试
'\\server\share\'
它类似于 Windows 驱动器的根目录。那是
C:\
,而不是C:
Looks like you're missing the trailing slash. Try
'\\server\share\'
It's similar to the root directory of a Windows drive. That's
C:\
, notC: