我正在为 sketch up 编写插件。我找不到从 Windows 上的 My Documents
目录追加文本文件的简单方法。我遇到的问题是:
- 使用当前用户名打开
- Windows 版本之间的差异。
我需要代码在 32 位计算机上的 Windows 7 上的 我的文档
中以追加模式打开文件。
最佳答案
您可以在 Ruby 中使用反引号 ` 对系统运行命令行。然后它将返回调用的结果。
这是官方描述 Ruby-Doc.org
Returns the standard output of running cmd in a subshell. The built-in syntax %x{...} uses this method. Sets $? to the process status.
`echo` #=> 'ECHO is on.' if ran on Windows
在 Windows 中,有 special variables您可以用来收集用户名等信息:
echo %username%
因此,您可以使用这两个来收集用户的“我的文档”文件夹的位置(前提是它们在 Windows 上)。
# Tested on Windows XP and 7
my_documents = File.join(`echo %userprofile%`.chomp, "My Documents")
注意:
Ruby 中还有一个进行系统调用的方法,它是 system
.但是,这会返回一个 bool 值,它不适用于您的情况。
另请注意,此解决方案仅适用于 Windows。
关于ruby - 在 Windows 上从 `My Documents` 打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14608816/