首页 > 解决方案 > 使用默认应用打开名称中包含空格(或 &)的文件

问题描述

我正在尝试打开一个文件,其路径为 C:\Users\kevin\Documents\LIN KED.png(或 C:\Users\kevin\Documents\LIN&KED.png)。文件路径将在linked_file_command 中。

我想在 Windows 上的默认应用程序中打开该文件。我想继续使用linked_file_command 字符串,只是为了有一个变量而不是硬编码的路径。

我尝试了以下方法:

1) system %{cmd /c "start #{linked_file_command}"}
(from https://stackoverflow.com/questions/9476291/how-to-open-file-in-default-application-ruby )

2) system('cmd /c start "" C:\Users\kevin\Documents\LIN KED.png')
(from https://www.ruby-forum.com/topic/209961 )

两者的结果:

Le système ne peut trouver le fichier C:\Users\kevin\Documents\LIN。(系统找不到文件C:\Users\kevin\Documents\LIN)

提前谢谢^.^

标签: javarubyjruby

解决方案


这应该工作

filePath = "c:\path\to\file name.jpg"
system %{cmd /c "start #{filePath}"}

推荐阅读