[英]Issue running .exe/.rb using C#
I am using below C# code to execute .exe/.rb and it works(Result: C# code initiate the .rb file and then the rb initiate the browser and launches into google). 我正在使用下面的C#代码执行.exe / .rb,并且它起作用(结果:C#代码启动.rb文件,然后rb启动浏览器并启动到google)。
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();
}
and here is the .rb file content, 这是.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
But when I try to use the same C# code to execute the below rb code it fails(Result: C# code initiate the .rb file and then the rb did not do anything, output file is not created neither the browser launch to google).. 但是,当我尝试使用相同的C#代码执行以下rb代码时,它会失败(结果:C#代码启动.rb文件,然后rb没有执行任何操作,也没有创建输出文件,也没有浏览器启动到google)。 。
Basically in the below rb code I have added logic to write the output to the .txt file. 基本上,在下面的rb代码中,我添加了将输出写入.txt文件的逻辑。 Directly executing the rb works but not through C#.
直接执行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
Am I missing something? 我想念什么吗? Please help.
请帮忙。
尝试更改参数以匹配Ruby的路径样式。
rubyProc.Arguments = "C:/Users/xxxxx/Downloads/ACN_Demo/sa.exe"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.