繁体   English   中英

使用C#运行.exe / .rb的问题

[英]Issue running .exe/.rb using C#

我正在使用下面的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(); 
            }

这是.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没有执行任何操作,也没有创建输出文件,也没有浏览器启动到google)。 。

基本上,在下面的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

我想念什么吗? 请帮忙。

尝试更改参数以匹配Ruby的路径样式。

rubyProc.Arguments = "C:/Users/xxxxx/Downloads/ACN_Demo/sa.exe"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM