Reputation: 1265
It was answered here calling a ruby script in c#
but does that work? I tried this but it keeps failing with "The system cannot find the file specified" error, I'm assuming its because of ruby command before the file name, but I'm not quite sure.
Thanks for the help
Upvotes: 2
Views: 5246
Reputation: 313
Try this
void runScript()
{
using (Process p = new Process())
{
ProcessStartInfo info = new ProcessStartInfo("ruby");
info.Arguments = "C:\rubyscript.rb args"; // set args
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
string output = p.StandardOutput.ReadToEnd();
// process output
}
}
Upvotes: 0
Reputation: 3070
Here is my code for running a ruby script.
using (var proc = new Process())
{
var startInfo = new ProcessStartInfo(@"ruby");
startInfo.Arguments = filePath;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
proc.StartInfo = startInfo;
proc.Start();
}
This method runs it asynchronously
, since the script may take an unknown amount of time to run this allows the main
thread
to keep running without locking it, and then waits for the script to finish running before returning the Task
.
private async Task RunRubyScript(string filePath)
{
await Task.Run(() =>
{
using (var proc = new Process())
{
var startInfo = new ProcessStartInfo(@"ruby");
startInfo.Arguments = filePath;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
proc.StartInfo = startInfo;
proc.Start();
proc.WaitForExit();
}
});
}
Hope this helps!
Upvotes: 0
Reputation: 31428
You could also try to execute the Ruby code with IronRuby with something like this
using System;
using Microsoft.Scripting.Hosting;
using IronRuby;
class ExecuteRubyExample
{
static void Main()
{
ScriptEngine engine = IronRuby.Ruby.CreateEngine();
engine.ExecuteFile("C:/rubyscript.rb");
}
}
Upvotes: 4
Reputation: 6103
The linked answer looks reasonably proper, but it's obviously not working for you. That means it's probably one of two things.
1) the backslashes are biting you. Try changing
ProcessStartInfo info = new ProcessStartInfo("ruby C:\rubyscript.rb");
to
ProcessStartInfo info = new ProcessStartInfo(@"ruby C:\rubyscript.rb");
or
ProcessStartInfo info = new ProcessStartInfo("ruby C:\\rubyscript.rb");
The first change uses string literals, the second escapes the backslash properly.
2) the environment path isn't getting Ruby's bin directory exported to it. This is less likely and more of a pain to test for, so I'd focus on the first.
Upvotes: 1