Skip to content

Instantly share code, notes, and snippets.

@creativcoder
Created November 15, 2015 14:26
Show Gist options
  • Save creativcoder/6df4d349447ff1416e68 to your computer and use it in GitHub Desktop.
Save creativcoder/6df4d349447ff1416e68 to your computer and use it in GitHub Desktop.
Passing arguements from C# to python
import sys
print int(sys.argv[1]) + int(sys.argv[2])
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CallPython
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\Python27\\python.exe";
Console.Write(args.Length);
// arg[0] = Path to your python script (example : "C:\\add_them.py")
// arg[1] = first arguement taken from C#'s main method's args variable (here i'm passing a number : 5)
// arg[2] = second arguement taken from C#'s main method's args variable ( here i'm passing a number : 6)
// pass these to your Arguements property of your ProcessStartInfo instance
start.Arguments = string.Format("{0} {1} {2}",args[0],args[1],args[2]);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
// this prints 11
Console.Write(result);
}
}
Console.Read();
}
}
}
@Wizard2436829
Copy link

This was particularly helpful for me while I was working on AWS S3 when we are using Boto3 with C#. Keep up the good work.

@Peng-09-08
Copy link

Thanks for your sample code.

@sophada
Copy link

sophada commented Jan 28, 2021

Great

@AbdulhaleemSiddiqui
Copy link

thanks bro

@okabasakal88
Copy link

Really helpful for me thx

@androbro
Copy link

androbro commented Jan 5, 2022

i have this issue: "The specified executable is not a valid application for this OS platform". Someone an idea what to do?

@mchtilianov
Copy link

I fucking love u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment