troubleshooting Question

Write a C# Command Line Wrapper for Ruby

Avatar of openshac
openshac asked on
C#Ruby
3 Comments1 Solution1149 ViewsLast Modified:
I am trying to write a C# wrapper for a Command Line tool which is written in Ruby.
I have no experience of Ruby but I am able to run this tool successfully from the command line with the following command:

C:> C:\Progra~1\ruby\bin\ruby.exe C:\MyRubyCLTool abc123

where MyRubyCLTool is the Ruby command line tool
and abc123 is a string parameter.

I have used the following Microsoft Article (How To Write a Wrapper for a Command-Line Tool with Visual C# .NET) at http://support.microsoft.com/kb/305994 
But I get no ouput at the line:

output = outputStream.ReadToEnd();

I have checked my C# wrapper and it works for simple command like ipconfig etc so I am resonably confident that it working OK.

Is there something specific I need to do for Ruby?

I have given my code below which I am running with the following arguments:
LaunchEXE.Run(@"C:\Progra~1\ruby\bin\ruby.exe", "C:\MyRubyCLTool abc123", 100);
 
class LaunchEXE
    {
        internal static string Run(string exeName, string argsLine, int timeoutSeconds)
        {
            StreamReader outputStream = StreamReader.Null;
            string output = string.Empty;
 
            bool success = false;
            
            try
            {
                Process newProcess = new Process();
                newProcess.StartInfo.FileName = exeName;
                newProcess.StartInfo.Arguments = argsLine;
                newProcess.StartInfo.UseShellExecute = false;
                newProcess.StartInfo.CreateNoWindow = true;
                newProcess.StartInfo.RedirectStandardOutput = true;
                newProcess.Start();
 
 
                if (0 == timeoutSeconds)
                {
                    outputStream = newProcess.StandardOutput;
                    output = outputStream.ReadToEnd();
                    newProcess.WaitForExit();
                }
                else
                {
                    success = newProcess.WaitForExit(timeoutSeconds * 1000);
                }
 
                if (success)
                {
                    outputStream = newProcess.StandardOutput;
                    output = outputStream.ReadToEnd();
                }
                else
                {
                    output = "Timed out at " + timeoutSeconds + " seconds waiting for " + exeName + " to exit.";
                }
            }
 
            catch (Exception e)
            {
                throw (new Exception("An error occurred running " + exeName + ".", e));
            }
            finally
            {
                outputStream.Close();
            }
 
            return "\t" + output;
        }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros