calling a ruby script in c#

后端 未结 2 1733
-上瘾入骨i
-上瘾入骨i 2020-12-11 09:07

How do make a call to a ruby script and pass some parameters and once the script is finished return the control back to the c# code with the result?

相关标签:
2条回答
  • 2020-12-11 09:31

    Just to fill smaller gaps I've implemented the same functionallity with ability to access OutputStream asynchronously.

     
       public void RunScript(string script, string arguments, out string errorMessage)
       {
          errorMessage = string.empty;
          using (Process process = new Process())
          {
              process.OutputDataReceived += process_OutputDataReceived;
              ProcessStartInfo info = new ProcessStartInfo(script);
              info.Arguments = String.Join(" ", arguments);
              info.UseShellExecute = false;
              info.RedirectStandardError = true;
              info.RedirectStandardOutput = true;
              info.WindowStyle = ProcessWindowStyle.Hidden;
              process.StartInfo = info;
              process.EnableRaisingEvents = true;
              process.Start();
              process.BeginOutputReadLine();
              process.WaitForExit();
              errorMessage = process.StandardError.ReadToEnd();
         }
      }
      private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
      {
         using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
         {
             if (!string.IsNullOrEmpty(e.Data))
             {
                  // Write the output somewhere
              }
          }
      }
    

    0 讨论(0)
  • 2020-12-11 09:56
        void runScript()
        {
            using (Process p = new Process())
            {
                ProcessStartInfo info = new ProcessStartInfo("ruby C:\rubyscript.rb");
                info.Arguments = "args"; // set args
                info.RedirectStandardInput = true;
                info.RedirectStandardOutput = true;
                info.UseShellExecute = false;
                p.StartInfo = info;
                p.Start();
                string output = p.StandardOutput.ReadToEnd();
                // process output
            }
        }
    
    0 讨论(0)
提交回复
热议问题