[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1389
  • Last Modified:

Run a Ruby .rb from C# Console app

Hello experts!

I have a C# console app that parses through data in a UDB database and inserts it into a SQL database which works fine. I also have a ruby script that parses a text file and inserts data into the same SQL db. The ruby script also runs without error when run on its own.

I know it would have been more simple to just do both from the c# app, however, I wrote the script in Ruby 1.9.2 for the pure fact of learning the lang. The c# app is dependent on the data imported by the Ruby script becuase of key constraints.

Soooo, I am simply trying to run the ruby script from the c# app. It runs without error -- but it appears the ruby script is never being processed?

Also it never opens a new CMD window, which I would like it to do and then parse the txt file -- Not sure if this is possible? Or if it can show the ruby print statements in the current command window?

I have attempted a few variations of the following?

Any suggestions?!
Thanks in advance!
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(@"ruby")
{
   FileName = "cmd.exe";
   Arguments = @"C:\\connextToDB.rb";
   RedirectStandardInput = true;
   RedirectStandardOutput = true;
   UseShellExecute = false;
   WindowStyle = ProcessWindowStyle.Normal;
   CreateNoWindow = false;
}

System.Diagnostics.Process process = new System.Diagnostics.Process()
{ 
    StartInfo = startInfo 
};
process.Start();
process.StandardInput.WriteLine("exit");
process.WaitForExit();

Open in new window

0
I_s
Asked:
I_s
  • 4
  • 4
1 Solution
 
ItpotterCommented:
Have you got environment variables set for ruby on the machine?
0
 
käµfm³d 👽Commented:
Do you have .rb files registered to run with the Ruby interpreter executable? If not, you will probably have to pass the script to the executable using Process.Start. For example:

FileName = "ruby.exe";

Open in new window


Everything else should be fine.
0
 
I_sAuthor Commented:
Ok so I got it to work, ...half way..

I moved the .rb and .txt files into the project bin folder of my c# app and the following code will execute the process.

Now i am trying to recieve the Output using StreamReader and StandardOutput, but it is not returning any statements from the ruby code. I have multiple 'puts' statements in the ruby code which I would like to see as it runs through the script.

Is that possible?

Here is working code:
Console.WriteLine();
                            Console.WriteLine("Processing Ruby text converter.");
                            try
                            {
                                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                                startInfo.FileName = "ruby.exe";
                                startInfo.Arguments = @"connectToDB.rb";
                                startInfo.RedirectStandardInput = true;
                                startInfo.RedirectStandardOutput = true;
                                startInfo.UseShellExecute = false;
                                startInfo.CreateNoWindow = true;

                                System.Diagnostics.Process process = new System.Diagnostics.Process()
                                {
                                    StartInfo = startInfo
                                };

                                if (!process.Start())
                                {
                                    Console.WriteLine("Fail");
                                }
                                StreamReader reader = process.StandardOutput;
                                string output = reader.ReadLine();
                                Console.WriteLine(output);       //<--------- Does not return anything
                                process.WaitForExit();
                                Console.WriteLine();
                                Console.WriteLine("Ruby conversion complete.");
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
I_sAuthor Commented:
@kaufmed

Thanks for the reply, I did find that I needed to use the ruby.exe as the filename -- is this the reason I cannot view the output? becuase I am not invoking a command prompt?

Thanks
0
 
käµfm³d 👽Commented:
is this the reason I cannot view the output? becuase I am not invoking a command prompt?
When you use Process.Start you will only see the command prompt (if you didn't explicitly hide it) for as long as it takes the process to execute--then it terminates. If you want the command prompt to stay, then yes, you have to explicitly call cmd.exe. There is an argument of /k that causes cmd.exe not to close when a process is finished executing. Here is how you could use that option:

startInfo.FileName = "cmd.exe";
startInfo.Arguments = @"/k ruby.exe connectToDB.rb";

Open in new window


You will have to set CreateNoWindow to false though.
0
 
I_sAuthor Commented:
I modified my code using your input, this is the code now.

It still does not create a new command prompt window and enter the argument I pass?

I apprectiate your help


System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo()
                                {
                                    FileName = "cmd.exe",
                                    Arguments = @"/k ruby.exe connectToDB.rb",
                                    RedirectStandardInput = true,
                                    RedirectStandardOutput = true,
                                    UseShellExecute = false,
                                    CreateNoWindow = false
                                };

                                System.Diagnostics.Process process = new System.Diagnostics.Process()
                                {
                                    StartInfo = startInfo
                                };

                                if (!process.Start())
                                {
                                    Console.WriteLine("Process Failed to Start...");
                                }
                                process.WaitForExit();

Open in new window

0
 
käµfm³d 👽Commented:
Oh, you may also want to remove lines 5 & 6--I'm not sure why you redirected. It's fine if you need it, but for testing you may consider removing it.

Here is my test using your exact code (adjusted for my comments):

using System;

namespace _27418923
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo()
            {
                FileName = "cmd.exe",
                Arguments = @"/k ruby.exe connectToDB.rb",
                UseShellExecute = false,
                CreateNoWindow = false
            };

            System.Diagnostics.Process process = new System.Diagnostics.Process()
            {
                StartInfo = startInfo
            };

            if (!process.Start())
            {
                Console.WriteLine("Process Failed to Start...");
            }

            process.WaitForExit();
        }
    }
}

Open in new window


...and here is my connectToDB.rb file:

print "Hello World!"

Open in new window


...and here is my output:

Screenshot
0
 
I_sAuthor Commented:
Works great!

Thanks for the help, removing that redirect line allowed me to see the print statements in the c# console.

I appreciate your time on my behalf.
0
 
käµfm³d 👽Commented:
NP. Glad to help  = )
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now