I want to call my python program and get it executed automatically as it gets called, using c#. I have done uptill opening the program but how to run it and the get the output. It is my final year project kindly help me out.Here is my code:
Process p = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.UseShellExecute = true;
pi.FileName = #"python.exe";
p.StartInfo = pi;
try
{
p.StandardOutput.ReadToEnd();
}
catch (Exception Ex)
{
}
The following code execute python script that call modules and return result
class Program
{
static void Main(string[] args)
{
RunPython();
Console.ReadKey();
}
static void RunPython()
{
var args = "test.py"; //main python script
ProcessStartInfo start = new ProcessStartInfo();
//path to Python program
start.FileName = #"F:\Python\Python35-32\python.exe";
start.Arguments = string.Format("{0} ", args);
//very important to use modules and other scripts called by main script
start.WorkingDirectory = #"f:\labs";
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
Console.Write(result);
}
}
}
}
test scripts:
test.py
import fibo
print ( "Hello, world!")
fibo.fib(1000)
module: fibo.py
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print (b),
a, b = b, a+b
Related
I have c# program that I want to run but from the cmd using a command line so here is my question I don't know how to create the command and I don't know how to send the parameter in my command to some function there.
You can use the Process class to execute files
var fileName = "some.exe";
var arguments = "";
var info = new System.Diagnostics.ProcessStartInfo(fileName, arguments);
info.UseShellExecute = false;
info.CreateNoWindow = true;
// if you want read output
info.RedirectStandardOutput = true;
var process = new System.Diagnostics.Process { StartInfo = info };
process.Start();
var output = process.StandardOutput.ReadToEnd();
var error = process.StandardError?.ReadToEnd();
You can create a console application and after generating (by building) .exe file, you can call with the arguments in command line.
Sample Example:
class Program
{
static void Main(string[] args)
{
var a = Convert.ToInt32(args[0]);
var b = Convert.ToInt32(args[1]);
Console.WriteLine(a+b);
}
}
OUTPUT
I'm trying to write a program which executes 2 different .exe files and outputs their results to a text file. When I execute them separately, they work fine, but when I try to execute them both, the second process doesn't run. Can anyone help?
Here is the code. Player1.exe and Player2.exe are console applications returning 0 or 1.
static void Main(string[] args)
{
Process process1 = new Process();
process1.StartInfo.FileName = "C:/Programming/Tournament/Player1.exe";
process1.StartInfo.Arguments = "";
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
process1.Start();
var result1 = process1.StandardOutput.ReadToEnd();
process1.Close();
Process process2 = new Process();
process2.StartInfo.FileName = "C:/Programming/Tournament/Player2.exe";
process2.StartInfo.Arguments = "";
process2.StartInfo.UseShellExecute = false;
process2.StartInfo.RedirectStandardOutput = true;
process2.Start();
string result2 = process2.StandardOutput.ReadToEnd().ToString();
process2.Close();
string resultsPath = "C:/Programming/Tournament/Results/Player1vsPlayer2.txt";
if (!File.Exists(resultsPath))
{
StreamWriter sw = File.CreateText(resultsPath);
sw.WriteLine("Player1 " + "Player2");
sw.WriteLine(result1 + " " + result2);
sw.Flush();
}
}
1.
you wrote in a comment: "The program doesn't even reach to process2. I tested that by putting a breakpoint."
process1.Start() may be throwing an exception. Rather than setting a breakpoint at process2, step through the lines and find the exception. Or better yet, add a try/catch block and report an error.
2.
Another possibility is that ReadToEnd is not behaving as expected. You can Peek and see if there is more data to read by looping like this:
var result1 = new StringBuilder()
while (process1.StandardOutput.Peek() > -1)
{
result1.Append(process1.StandardOutput.ReadLine());
}
3.
If these processes don't immediately provide data and end, then you need to get them both started before you begin waiting on their StandardOutput. Call Start() on each process before doing the reads.
I don't know too much about using process, but I use this approach when I need separate things to run at once. I didn't pull in the bottom portion of your project, but check this out to see if it helps in any way.
class Program
{
static void Main(string[] args)
{
Process process1 = new Process();
process1.StartInfo.FileName = "C:/Programming/Tournament/Player1.exe";
process1.StartInfo.Arguments = "";
process1.StartInfo.UseShellExecute = false;
process1.StartInfo.RedirectStandardOutput = true;
Process process2 = new Process();
process2.StartInfo.FileName = "C:/Programming/Tournament/Player2.exe";
process2.StartInfo.Arguments = "";
process2.StartInfo.UseShellExecute = false;
process2.StartInfo.RedirectStandardOutput = true;
Thread T1 = new Thread(delegate() {
doProcess(process1);
});
Thread T2 = new Thread(delegate() {
doProcess(process2);
});
T1.Start();
T2.Start();
}
public static void doProcess(Process myProcess)
{
myProcess.Start();
var result1 = myProcess.StandardOutput.ReadToEnd();
myProcess.Close();
}
}
I want to using C# process to get result from Command Prompt. The command is "java HelloWorld 1" (I have been build it to HelloWorld.class file using "javac HelloWorld.java")
The java code:
public class HelloWorld {
public HelloWorld() {}
public static void main(String[] args) { System.out.println("STARTED");
try {
int param = Integer.parseInt(args[0].toString());
if (param == 1) {
System.out.println("BASE 64!");
} else if (param == 2) {
System.out.println("MD5!");
} else {
System.out.println("INPUT NOT MATCH!");
}
} catch (Exception ee) {
System.out.println("NO INPUT - ERROR");
}
} }
and The C# code:
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Program Files\\Java\\jdk1.6.0_25\\bin\\java.exe";
p.StartInfo.Arguments = "HelloWorld 1";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
But the strOutput is "".
Can you give me the solution?
Thank you!
The most likely thing is that java cannot find your HelloWorld.class file.
In that case, it will write to the standard error something like
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
and then a stack trace.
And it will not write anything to the standard output
I suggest you do two things:
1) read from standard error as well and see what that says
2) add a classpath argument prior to the class file
p.StartInfo.Arguments = " -cp C:\\code\\myapp HelloWorld 1";
(Obviously substituting in the correct path to the folder that contains HelloWorld.class)
Another alternative is to set your process start info to the location of HelloWorld:
ProcessStartInfo pInfo = new ProcessStartInfo(#"C:\Program Files\Java\jdk1.6.0_25\bin\java.exe");
pInfo.Arguments = = "HelloWorld";
pInfo.WorkingDirectory = #"C:\JavaFiles";
pInfo.UseShellExecute = false;
pInfo.RedirectStandardOutput = true;
Process javaProc = Process.Start(pInfo);
string output = javaProc.StandarOutput.ReadToEnd();
EDIT: I just realized your p was a Process object. WorkingDirectory is a property of ProcessStartInfo
This is assuming that C:\JavaFiles\HelloWorld.class and C:\JavaFiles\HelloWorld.java exists. You should also follow Greg's advice and read StandardError, as it will help you troubleshoot future problems
I have the following:
C:\temp\dowork.exe < input.txt
processing.......
complete
C:\
I try this:
processArguments = " < input.txt";
pathToExe = "C:\\temp\dowork.exe";
startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(pathToExe),
Arguments = processArguments
};
try
{
using (_proc = Process.Start(startInfo))
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
and my dowork.exe crashes after Start() is called.
Any suggestions?
Post Question Update.
Thank you everyone for your input. I solved the problem using amit_g's answer. Extended thanks to Phil for showing likely the best way (although I didn't test it out, I can see why it is better). Below is my complete solution. Feel free to copy and modify for your own issue.
1) create a console application project, add this class
internal class DoWork
{
private static void Main(string[] args)
{
var fs = new FileStream("C:\\temp\\output.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None);
var toOutput = "Any items listed below this were passed in as args." + Environment.NewLine;
foreach (var s in args)
toOutput += s + Environment.NewLine;
Console.WriteLine("I do work. Please type any letter then the enter key.");
var letter = Console.ReadLine();
Console.WriteLine("Thank you.");
Thread.Sleep(500);
toOutput += "Anything below this line should be a single letter." + Environment.NewLine;
toOutput += letter + Environment.NewLine;
var sw = new StreamWriter(fs);
sw.Write(toOutput);
sw.Close();
fs.Close();
}
}
2) Create 1 file: C:\temp\input.txt
3) edit input.txt, type a single letter 'w' and save (that's right the file contains a single letter).
4) Create a new class library project. Add a reference to nunit (i'm using version 2.2).
5) Create a testfixture class, it should look like the following. Note: this test fixture is handling external resources, as such you cannot run the entire fixture, instead, run each test one-at-a-time. You can fix this by making sure all file streams are closed, but i didn't care to write this, feel free to extend it yourself.
using System.Diagnostics;
using System.IO;
using NUnit.Framework;
namespace Sandbox.ConsoleApplication
{
[TestFixture]
public class DoWorkTestFixture
{
// NOTE: following url explains how ms-dos performs redirection from the command line:
// http://www.febooti.com/products/command-line-email/batch-files/ms-dos-command-redirection.html
private string _workFolder = "C:\\Temp\\";
private string _inputFile = "input.txt";
private string _outputFile = "output.txt";
private string _exe = "dowork.exe";
[TearDown]
public void TearDown()
{
File.Delete(_workFolder + _outputFile);
}
[Test]
public void DoWorkWithoutRedirection()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder
};
var process = Process.Start(startInfo);
process.WaitForExit();
Assert.IsTrue(File.Exists(_workFolder + _outputFile));
}
[Test]
public void DoWorkWithoutRedirectionWithArgument()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder,
Arguments = _inputFile
};
var process = Process.Start(startInfo);
process.WaitForExit();
var outputStrings = File.ReadAllLines(_workFolder + _outputFile);
Assert.IsTrue(File.Exists(_workFolder + _outputFile));
Assert.AreEqual(_inputFile, outputStrings[1]);
}
[Test]
public void DoWorkWithRedirection()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder,
RedirectStandardInput = true
};
var myProcess = Process.Start(startInfo);
var myStreamWriter = myProcess.StandardInput;
var inputText = File.ReadAllText(_workFolder + _inputFile);
myStreamWriter.Write(inputText);
// this is usually needed, not for this easy test though:
// myProcess.WaitForExit();
var outputStrings = File.ReadAllLines(_workFolder + _outputFile);
Assert.IsTrue(File.Exists(_workFolder + _outputFile));
// input.txt contains a single letter: 'w', it will appear on line 3 of output.txt
if(outputStrings.Length >= 3) Assert.AreEqual("w", outputStrings[2]);
}
[Test]
public void DoWorkWithRedirectionAndArgument()
{
var startInfo = new ProcessStartInfo
{
FileName = _workFolder + _exe,
UseShellExecute = false,
WorkingDirectory = _workFolder,
RedirectStandardInput = true
};
var myProcess = Process.Start(startInfo);
var myStreamWriter = myProcess.StandardInput;
var inputText = File.ReadAllText(_workFolder + _inputFile);
myStreamWriter.Write(inputText);
myStreamWriter.Close();
// this is usually needed, not for this easy test though:
// myProcess.WaitForExit();
var outputStrings = File.ReadAllLines(_workFolder + _outputFile);
Assert.IsTrue(File.Exists(_workFolder + _outputFile));
// input.txt contains a single letter: 'w', it will appear on line 3 of output.txt
Assert.IsTrue(outputStrings.Length >= 3);
Assert.AreEqual("w", outputStrings[2]);
}
}
}
You have to use the STDIN redirection. Like this...
inputFilePath = "C:\\temp\input.txt";
pathToExe = "C:\\temp\dowork.exe";
startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(pathToExe),
RedirectStandardInput = true
};
try
{
using (_proc = Process.Start(startInfo))
{
StreamWriter myStreamWriter = myProcess.StandardInput;
// Use only if the file is very small. Use stream copy (see Phil's comment).
String inputText = File.ReadAllText(inputFilePath);
myStreamWriter.Write(inputText);
}
_proc.WaitForExit();
}
catch (Exception e)
{
Console.WriteLine(e);
}
You are going to want to do something like,
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = pathToExe,
UseShellExecute = false,
WorkingDirectory = FilepathHelper.GetFolderFromFullPath(pathToExe)
};
Process process = Process.Start(startInfo);
FileStream reader = File.OpenRead("input.txt");
reader.CopyTo(process.StandardInput.BaseStream);
This can be done, at least in a somewhat "hacky" fashion by passing arguments directly to cmd.exe. However, as I recommend emulating the "<" manually as in the other answers, this is here as a note only.
(foo.txt is contains two lines, "b" and "a", so that they will be reversed when correctly sorted)
var x = new ProcessStartInfo {
FileName = "cmd",
Arguments = "/k sort < foo.txt",
UseShellExecute = false,
};
Process.Start(x);
You can replace /k with /c to prevent cmd.exe from remaining open. (See cmd /? for the options).
Happy coding.
First, you don't need a space preceeding your args. The method does it for you. This might mess you up to begin with. So it would be:
processArguments = "< input.txt";
But if that doesn't work you can try::
process = "cmd.exe";
processArguments = "/c dowork.exe < input.txt";
I tried using the Process class as always but that didn't work. All I am doing is trying to run a Python file like someone double clicked it.
Is it possible?
EDIT:
Sample code:
string pythonScript = #"C:\callme.py";
string workDir = System.IO.Path.GetDirectoryName ( pythonScript );
Process proc = new Process ( );
proc.StartInfo.WorkingDirectory = workDir;
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = pythonScript;
proc.StartInfo.Arguments = "1, 2, 3";
I don't get any error, but the script isn't run. When I run the script manually, I see the result.
Here's my code for executing a python script from C#, with a redirected standard input and output ( I pass info in via the standard input), copied from an example on the web somewhere. Python location is hard coded as you can see, can refactor.
private static string CallPython(string script, string pyArgs, string workingDirectory, string[] standardInput)
{
ProcessStartInfo startInfo;
Process process;
string ret = "";
try
{
startInfo = new ProcessStartInfo(#"c:\python25\python.exe");
startInfo.WorkingDirectory = workingDirectory;
if (pyArgs.Length != 0)
startInfo.Arguments = script + " " + pyArgs;
else
startInfo.Arguments = script;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
process = new Process();
process.StartInfo = startInfo;
process.Start();
// write to standard input
foreach (string si in standardInput)
{
process.StandardInput.WriteLine(si);
}
string s;
while ((s = process.StandardError.ReadLine()) != null)
{
ret += s;
throw new System.Exception(ret);
}
while ((s = process.StandardOutput.ReadLine()) != null)
{
ret += s;
}
return ret;
}
catch (System.Exception ex)
{
string problem = ex.Message;
return problem;
}
}
Process.Start should work. if it doesn't, would you post your code and the error you are getting?
You forgot proc.Start() at the end. The code you have should work if you call Start().