Skip to content
loading...
markdown guide
 
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Process process = new Process())
            {
                /* Assuming python is in your PATH */
                process.StartInfo.FileName = "python.exe";
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.Arguments = "yourpythonscript.py someotherargument";
                process.Start();

                // Synchronously read the standard output of the spawned process. 
                StreamReader reader = process.StandardOutput;
                string output = reader.ReadToEnd();

                // Write the redirected output to this application's window.
                Console.WriteLine(output);

                process.WaitForExit();
            }
        }
    }
}
 

It would be cool if you could excute the process with options and then just get the result of stdout pre-read, something like this for example:

using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace ConsoleApp
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using (Process process = new Process())
            {
                var output = await process.execute("python.exe script.py --arg value", new {
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                });
                Console.WriteLine(output);
            }
        }
    }
}
 
 

Reading this, I'm surprised the .NET community hasn't created a Thread abstraction so you could async/await a process execution.

 
 
Classic DEV Post from Mar 16

Loneliness in tech

I'll preface this with a quick bit about me: I've been a remote worker for just over 5 years now, typ...

rohanThegreaTT profile image
I'm 13 years old and love to code, fan of python, c# and javascript.