I'm currently creating an application in C# that allows you to create a server and manage it easily, to do this it uses batch files to run said servers. It works by creating batch files and using those to run the server. (Java by the way).
So, what I'm wondering is if it's possible to grab the output from the console and rather than dumping it in a textbox and closing the console like the code below does, I need it to continually post any output from the console without closing and without spamming loads of consoles open (tried using a timer).
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo =
new ProcessStartInfo(batchfilelocation);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadToEnd();
textBox1.Text = textBox1.Text + myString;
myProcess.Close();
I'm also wondering if it would be possible to allow input from a windows forms control such as a button that would execute a command by entering it in the console and pressing enter for example. Or a textbox that allows you to do the same thing.
Thanks!
You don't need a TextBox control to store a string. Just use a string variable instead.
string concatenatedString = null;
concatenatedString += myStreamReader.ReadToEnd();
Secondly, you can do this without creating a console window. The code below will allow your program to run without seeing a ton of console windows popup.
myProcessStartInfo.CreateNoWindow = true;
Related
First of all I do not know if it is a bad practice to call python script from c# so if this is the case please tell me.My current problem is as follows.
MY c# code only runs the python script partially....
means (python script create only 4 files when it is supposed to create 10 files)
But When I run my script from cmd in windows I see complete functionality....
Another thing I saw is when I stop my Visual Studio(2013) I see the complete functionality
I am calling the python script(main.py) from c# like this...
public JsonResult FetchscrapyDataUrl(String website)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\ProgramData\Anaconda3\python.exe";
start.Arguments = #"C:\Users\PycharmProjects\scraping_web\scrape_info\main.py";
//this is path to .py file from scrapy project
start.CreateNoWindow = false; // We don't need new window
start.UseShellExecute = false; // Do not use OS shell
//start.RedirectStandardOutput = true;// Any output, generated by application will be redirected back
start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions)
Console.WriteLine("Python Starting");
start.RedirectStandardOutput = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script
string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test")
Console.Write(result);
}
}
}
Why I am getting complete script functionality when I stop in Visual Studio(2013)??
I dont understand you motivation behind this. But why dont you use IronPython which is excellent addition to the .NET Framework, providing Python developers with the power of the .NET framework.
I am having need to automate an external windows console application from C#. Application is basically interface to an external device. When I invoke application it will ask me for authentication ie to enter a password with prompt something like 'Enter password:'. Right now there is no way to configure this application to run without interactive password prompt.
So I want to automate same from C# by sending password whenever it prompts and then to fire come commands which will execute on external device and then grab output. I know about process class and I am having some pointers like I can use pipes for this purpose ( Not Sure ? ).
As I have not handled this kind of automation before I am looking for help / direction form this.
Thanks in Advance.
The way to do this is to use the Redirection members, e.g ProcessStartInfo.RedirectStandardInput Property
Process myProcess = new Process();
myProcess.StartInfo.FileName = "someconsoleapp.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.ErrorDialog = false;
myProcess.Start();
StreamWriter stdInputWriter = myProcess.StandardInput;
StreamReader stdOutputReader = myProcess.StandardOutput;
stdInputWriter.WriteLine(password);
var op = stdOutputReader.ReadLine();
// close this - sending EOF to the console application - hopefully well written
// to handle this properly.
stdInputWriter.Close();
// Wait for the process to finish.
myProcess.WaitForExit();
myProcess.Close();
I am writing a windows forms application in C#
I have a Process Object which runs a cmd command and returns it's output.
Process Pro = new Process();
Pro.StartInfo.FileName = "cmd.exe";
Pro.StartInfo.Arguments = "<Dos Command here>";
Pro.StartInfo.CreateNoWindow = true;
Pro.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
Pro.StartInfo.RedirectStandardOutput = true;
Pro.StartInfo.UseShellExecute = false;
Pro.Start();
Which works fine! However if the output of the command is not ASCII(in my case Greek), the Output are random symbols. Surely an encoding problem.
If i run the same code on a console application everything runs smoothly.
I tried reading the Base stream as UTF-8, but no luck!
System.IO.StreamReader Rdr = new System.IO.StreamReader(Pro.StandardOutput.BaseStream, Encoding.UTF8);
Is there any way to read the output properly in a winform application?
Thnx!
The real solution is base on this:
unicode-characters-in-windows-command-line-how
check here:
Wiki code page
for the code page you need.
you can also do an ugly hack, writing the command to a batch file (f.e foo.bat)
then running it as foo.bat > log.txt
then you can read the output from log.txt.
Has anyone ever created a PDF document from a TeX document using pdflatex.exe in their C#/WPF application? I have my TeX document and I want to convert it to PDF and display it within the application, however, I'm unsure how to go about doing this and there's virtually nothing that I can find online about doing something like this. Does anyone know what the best way to do something like this is (convert a TeX document to PDF via pdflatex.exe within a C# application)?
Thanks a lot!
Here's the code I used. It assumes the source is in the same folder as the executable, and includes running BibTeX if you need it (just exclude the second process if needed).
string filename = "<your LaTeX source file>.tex";
Process p1 = new Process();
p1.StartInfo.FileName = "<your path to>\pdflatex.exe";
p1.StartInfo.Arguments = filename;
p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p1.StartInfo.RedirectStandardOutput = true;
p1.StartInfo.UseShellExecute = false;
Process p2 = new Process();
p2.StartInfo.FileName = "<your path to>\bibtex.exe";
p2.StartInfo.Arguments = Path.GetFileNameWithoutExtension(filename);
p2.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.UseShellExecute = false;
p1.Start();
var output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
p2.Start();
output = p2.StandardOutput.ReadToEnd();
p2.WaitForExit();
p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
p1.Start();
output = p1.StandardOutput.ReadToEnd();
p1.WaitForExit();
Yes, this could be cleaned up a little, and certainly without the call to BibTeX could just be called in a loop (3 times is the recommended number to make sure all the references are correct). Also, there's no exception handling, so you might want to add try / catch blocks around the process calls, etc.
I have never done that, but this is perfectly possible. You'll need to use System.Diagnostics.Process class to run pdflatex.exe
The rest is about choosing the way this should run. You probably have some options here:
If pdflatex supports writing output to "standard output" you can intercept that and do with the content whatever you want.
Another option is using some temporary folder to write the file to and subscribing to notifications about folder changes (asynchronous, you can run multiple instances of pdflatex) or simply waiting for the process to finish (synchronous, you can run only one instance of pdflatex at a time).
I decided to do something really simple -
A WinForms application with a text box for C# code and a button. When the button is clicked, I save the content of the text box to a temp .cs file, invoke the csc.exe (Process.Start()) with the file name as parameter so that it is compiled. This is assuming I set the PATH variables and everything.
When the csc.exe outputs syntax errors and stuff, how can I get it back and show it in another text box in my app?
NOTE My aim is not to just "complie C# code from within my app (in whichever way possible)".. it is to get the output of csc.exe
You need to redirect stdout and stderr.
It goes along the lines of:
process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = command;
process.StartInfo.Arguments = arguments;
Then reading from process.StandardInput. I can't really remember if this is all you need.
Use csc.exe [parameters] >output.file to stream the console output of csc.exe to a text file, and then read the text file into your text box.
Use a ProcessStartInfo class to redirect the console output to a stream of yours, then display the stream content in your windows control.
You can also take a look a the CodeDom namespace to produce on the fly assemblies, but it depends on your goal and requirement.