我使用下面的C#代码来执行.exe / .rb并且它有效(结果:C#代码启动.rb文件,然后rb启动浏览器并启动到谷歌)。
using (Process p = new Process()) {
ProcessStartInfo rubyProc = new ProcessStartInfo("ruby");
rubyProc.Arguments = "C:\\Users\\xxxx\\Downloads\\ACN_Demo\\sa.exe";
// set args
rubyProc.RedirectStandardInput = true;
rubyProc.RedirectStandardOutput = true;
rubyProc.UseShellExecute = false;
p.StartInfo = rubyProc;
p.Start();
// process output
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();
//p.Kill();
}
这是.rb文件内容,
puts "0"
begin
require 'watir'
rescue Exception => e
puts "Exception: #{e}\n"
puts e.backtrace.join("\n\t")
end
begin
puts "1"
#fout = "result.txt"
puts "2"
#File.open(fout, 'w') do |f|
puts "3"
begin
br = Watir::Browser.new :ie
puts "4"
br.goto "https://google.com/"
br.wait
puts "5"
# f.write "Loaded the initial page. Terminating the program now..."
puts "6"
sleep 3
br.close
rescue Exception => e
# f.write "Exception: #{e}\n"
# f.write e.backtrace.join("\n\t")
end
# f.write "EOF\n"
#end
rescue Exception => e
puts "Exception: #{e}\n"
puts e.backtrace.join("\n\t")
end
puts "EOF"
gets
但是当我尝试使用相同的C#代码执行下面的rb代码时,它失败了(结果:C#代码启动.rb文件然后rb没有做任何事情,输出文件也没有创建浏览器启动到谷歌)..
基本上在下面的rb代码中我添加了将输出写入.txt文件的逻辑。直接执行rb有效但不能通过C#。
puts "0"
begin
require 'watir'
rescue Exception => e
puts "Exception: #{e}\n"
puts e.backtrace.join("\n\t")
end
begin
puts "1"
fout = "result.txt"
puts "2"
File.open(fout, 'w') do |f|
puts "3"
begin
br = Watir::Browser.new :ie
puts "4"
br.goto "https://google.com/"
br.wait
puts "5"
f.write "Loaded the initial page. Terminating the program now..."
puts "6"
sleep 3
br.close
rescue Exception => e
f.write "Exception: #{e}\n"
f.write e.backtrace.join("\n\t")
end
f.write "EOF\n"
end
rescue Exception => e
puts "Exception: #{e}\n"
puts e.backtrace.join("\n\t")
end
puts "EOF"
gets
我错过了什么吗?请帮忙。
答案 0 :(得分:0)
尝试更改参数以匹配Ruby的路径样式。
rubyProc.Arguments = "C:/Users/xxxxx/Downloads/ACN_Demo/sa.exe"