如何在 Windows 上的 Ruby 中寻址 UNC 路径?

发布于 2024-09-28 06:55:40 字数 423 浏览 11 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故乡的云 2024-10-05 06:55:40

尝试用另一个 '\' 转义 '\'

Dir.entries('\\\\\\\\192.168.10.1\\\\share')

Try to escape '\' with another '\'

Dir.entries('\\\\\\\\192.168.10.1\\\\share')
向日葵 2024-10-05 06:55:40

Ruby 以 POSIX 方式 解释路径,这意味着您应该尽可能使用正斜杠。

//server/share

尾部斜杠是不必要的,就像在本机 Windows 中一样。您可以使用反斜杠,但必须使用另一个反斜杠转义

\\\\server\\share

我只建议当您直接从本机程序传递 UNC 路径并且无法转换它们时。当我混合 Ruby/Windows 路径时,例如在使用 Ruby 方法和本机 Windows 应用程序的构建脚本中,每个路径都需要不同的路径,我将使用一些帮助器:

def windows_path(value)
  value.gsub '/', '\\'
end

def posix_path(value)
  value.gsub '\\', '/'
end

如果路径是文字,则始终将路径用单引号引起来,或者如果要插值则用双引号。正斜杠告诉 Ruby 开始解释正则表达式。这是我在 irb 中常见的错误。

irb> File.exists? //server/share
SyntaxError: (irb):2: unknown regexp options - rvr

Ruby interprets paths in a POSIX way, meaning you should use forward slashes when possible.

//server/share

The trailing slash is unnecessary, just like in native Windows. You can use backslashes, but they have to be escaped with another backslash.

\\\\server\\share

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:

def windows_path(value)
  value.gsub '/', '\\'
end

def posix_path(value)
  value.gsub '\\', '/'
end

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.

irb> File.exists? //server/share
SyntaxError: (irb):2: unknown regexp options - rvr
爱要勇敢去追 2024-10-05 06:55:40

看起来您缺少尾部斜杠。尝试 '\\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:\, not C:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文