![](/img/trans.png)
[英]How to run my ruby application using ruby extension library (in c), in windows?
[英]How to automate Windows Run application using Ruby
我需要從Ruby腳本中打開“運行”,然后鍵入文件的位置,然后單擊“確定”。 我已經看到一些打開記事本和使用WIN32OLE輸入文本的示例,但是我不確定如何打開“運行”命令。
如果您使用的是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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.