如何使用Ruby自动运行Windows Run应用程序

我需要打开从我的Ruby脚本运行并键入文件的位置,然后单击确定。 我已经看到一些例子来打开记事本和使用WIN32OLEinput文本,但我不知道如何打开运行命令。

如果你使用Windows,我认为你可以这样做:

`start location_of_my_file` 

你可以用ruby中的以下命令来完成这个任务

1) exec

2)使用反码或%x

3) system

随着文件的名称,你也应该给应该执行它的程序的名称。

例如:如果你想打开计算器,那么你可以做

 exec 'calc' # or `calc` or %x(calc) or system 'calc' 

例如:如果你想在记事本中打开一个文本文件,那么:

 exec 'notepad file_name.txt' 

要么

 `notepad file_name.txt` 

要么

 %x(notepad file_name.txt) 

要么

 system 'notepad file_name.txt' 

这是你可以做的一个方法:

 require 'win32ole' def power wsh = WIN32OLE.new('Wscript.Shell') if not wsh.AppActivate('powershell') wsh.Run('powershell') sleep(3) wsh.SendKeys('gwmi win32_bios{ENTER}') wsh.SendKeys('gwmi win32_processor{ENTER}') wsh.SendKeys('gwmi win32_volume{ENTER}') wsh.SendKeys('ls{ENTER}') wsh.SendKeys('ping 192.168.0.14{ENTER}') wsh.SendKeys('exit') end end power