Running Python Scripts from C# Applications

UI Implemantation

The intefrace includes three text boxes named txt_InputX, txt_InputY, and txt_Output, along with a button named btn_Execute.

using System;
using System.Windows.Forms;

namespace ScriptRunnerApp
{
    public partial class MainForm : Form
    {
        private PythonExecutor _executor;

        public MainForm()
        {
            InitializeComponent();
            _executor = new PythonExecutor();
            _executor.OnResultReceived += UpdateOutput;
        }

        private void btn_Execute_Click(object sender, EventArgs e)
        {
            ExecuteScript();
        }

        private void ExecuteScript()
        {
            int valX = int.Parse(txt_InputX.Text);
            int valY = int.Parse(txt_InputY.Text);

            string[] parameters = { valX.ToString(), valY.ToString() };
            string scriptPath = @"C:\scripts\math_ops.py";
            
            _executor.LaunchScript(scriptPath, parameters);
        }

        private void UpdateOutput(string message)
        {
            if (this.txt_Output.InvokeRequired)
            {
                OutputHandler handler = new OutputHandler(UpdateOutput);
                this.Invoke(handler, new object[] { message });
            }
            else
            {
                this.txt_Output.Text = message;
            }
        }
    }
}

Python Execusion Logic

using System;
using System.Diagnostics;

namespace ScriptRunnerApp
{
    public class ResultArgs : EventArgs
    {
        public string Message { get; set; }
        public ResultArgs(string msg) { Message = msg; }
    }

    delegate void OutputHandler(string text);

    class PythonExecutor
    {
        public string LastResult { get; private set; }
        public event OutputHandler OnResultReceived;

        public void LaunchScript(string scriptPath, params string[] arguments)
        {
            string interpreter = @"C:\python\python.exe";
            string fullArgs = $"\"{scriptPath}\" {string.Join(" ", arguments)} -u";

            Process proc = new Process();
            proc.StartInfo.FileName = interpreter;
            proc.StartInfo.Arguments = fullArgs;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardInput = true;

            proc.Start();
            proc.BeginOutputReadLine();
            proc.OutputDataReceived += ProcessOutput;
        }

        private void ProcessOutput(object sender, DataReceivedEventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(e.Data))
            {
                Console.WriteLine($"Script Output: {e.Data}");
                LastResult = e.Data;
            }

            OnResultReceived?.Invoke(LastResult);
        }
    }
}

Sample Python Script (math_ops.py)

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys

x = int(sys.argv[1])
y = int(sys.argv[2])

print(x * y)

Tags: C# python Process.Start WinForms Interop

Posted on Sun, 31 May 2026 00:12:25 +0000 by aragon1337