Apologies for asking a question that's been asked so many times before, but I'm really suck.
I'm trying start a Python process from C#. I want to pass data from my C# program to a Python script, perform some IO in the Python script (it will be SFTP operations, but for simplicity's sake for now I am just returning text), and return the output to C#.
I have tried doing this with IronPython, but the Python library I will be using (Paramiko) is not supported. I have also attempted some version of this with PythonNet with no luck. I would like to stick to using C# processes for now. I know it's possible based on other posts I've seen on here, but I really can't figure out what I've got wrong.
The final C# program needs to be a class library. But for now as I test, I've configured the output type as Windows Application in order to get output in the debug window (Right click on class -> Application -> general -> output type -> Windows Application). The program has been set as the Startup Project.
When I run the code, the debugging window and tools pop up for a second, making me think it does call the script, but I don't get any output redirected to the debug window. At the end, it reads:
The program '[38860] ExecuteProcess.exe' has exited with code 0 (0x0).
C# code
the following code is based on this: How do I run a Python script from C#?
using System.Diagnostics;
namespace ExecuteProcess
{
public class Class1
{
static void Main(string[] args)
{
string pythonExe = @"absolute_path_to_python.exe";
string scriptDir = @"absolute_path_to_script_to_be_called_in_solution";
string scriptArgs = "Hello World";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = pythonExe;
start.Arguments = string.Format("{0} {1}", scriptDir, scriptArgs);
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
process.WaitForExit();
string result = reader.ReadToEnd();
Debug.WriteLine(result);
}
}
}
}
Python Code
print("In python script")
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("params", type='str', store=True)
args = parser.parse_args()
def func(params):
print(args.params)
return(args.params)
func(args.params)
I appreciate any input/help I can get. Thanks for reading, cheers.