简体   繁体   中英

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).

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,

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)..

Basically in the below rb code I have added logic to write the output to the .txt file. Directly executing the rb works but not through 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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