How to read/write to Process input/output in C# - c#

I have a console app that prompts for various pieces of information that I want to invoke from C# via the System.Diagnostics.Process.Start() method from a Windows Form app but I can't quite get it to work correctly. As a simple example lets say I want to capture FirstName, and LastName via a Windows Form application and then pass each to the console app dynamically like this:
Forms app launches, the user keys in the values for firstname and lastname and clicks a button to submit
it calls Process.Start for the console app
Console app writes out 'Enter your first name"
Forms app reads this prompt, identifies that it needs to respond with the FirstName value it has collected and writes it to the console app's output
Console app accepts the input and responds with the next prompt "Enter your last name"
Forms app reads this prompt, identifies that it needs to respond with the LastName value it has collected and writes it to the console app's output
Console app now has all the information it needs and continues on its merry way
thanks for any insight, this is not an area I have worked with much at all

From the WinForms app, you can read output from the console app with the StreamReader returned by the Process.StandardOutput property, and write input to the console app with the StreamWriter returned by the Process.StandardInput property.
BTW, did you develop the console app yourself (or have the project files)? If so, I'd suggest using a better form of IPC, such as making the console app a DLL intead of an EXE, or using something from WCF or something like that.

Look at Process.StandardOutput and Process.StandardInput and Process.StandardError.
You need to read from Process.StandardOutput. When you read " 'Enter your first name"" then you write the first name to Process.StandardInput and so on.
Note that the child process can write to its StandardError as well and you probably need to read from it in a different thread so that you can read concurrently from both
In Java there is expect4j that makes this easy. I am pretty sure I saw something on .net but but I am not able to find it right now.
EDIT: Creating a Child Process with Redirected Input and Output seems to be a good article on this topic on MSDN

Related

Open c# Application from another Application

I have a problem, I already have webbrowser in c# app, I want to send url from another app, and open my c# app and url what I sent from another application.
Any ideas, tutorials, anything. Thanks
So based on what I understand from your question, you want to pass URL by app1 and open app2 with that url in app2(webbrowser control).
You can use Commandline Arguments to pass data between process,
Refer this stack overflow link command line arguments to winform applications
You could use Process.Start to start the process:
System.Diagnostics.Process.Start("path/to/browser.exe", "https://sampleurl.org");
And in your application, depending if it is wpf, winforms or something else read the arg (there are plenty of questions how to do it on stackoverflow)
You could use Process.Start to start the process:
Process.Start("path/to/browser.exe", "https://sampleurl.org");
And in your c# program access it in the main

Firing an event in a tray-icon app when an exe is launched?

I'm trying to write a Tray-Icon application with WPF as a kind of add-on to a telephone software. However my only way of recieving data from said telephone software is by having it automatically execute a URI with parameters (command line or http). Essentially I can freely define the format similar to this:
trayapp.exe --num $caller.number --name $caller.name
When the telephone software recieves a call it'll automatically fill in the parameters and executes the target.
So basically I want a permanently active executable that's running in the background but my only way of getting the necessary info (like caller-number) is by passing it as a command line parameter.
Is there a good way to get the data that's being passed to a URI into my background application? It's not like I can just re-execute the tray-app, or can I? Communication via HTTP (locally!) seems overkill but I just don't know a windows-internal equivalent ...

Send a string to IE and IE execute the string but not have IE appears or the Console screen appear

I don't know if it is possible to do this in Windows Console Application (C#) but I really need something like this for my project.
I would like to send a string (or URL link) to the IE and have IE execute that string. However, since this string is static; what it does is just turn a machine ON and OFF via internet. So, in this case I have no interest in seeing the IE to popup/display nor the black screen console to appears when the application got executed.
I know in Console Application C# we can do something like this
Process.Start("iexplore.exe", #"http://myserver.com");
However, when I run the above line, the IE popup and so does the black screen console.
Is there a way to not have IE popup and the screen console to not popup, too?
Thanks.
I think you are going about this the wrong way. IE is merely a portal to web requests. Just make the web request call directly. using something like the new Web API or HttpWebRequest
As to making the console not pop up, maybe you should be creating a Windows Service instead of a console application? We would need more details on the end user. If this is more of an automated action, then a service would work. If it is something the user clicks on, then why is displaying the console a bad thing? Typically, when you click on something you expect some sort of visual representation. But, here is a SO that answers this question already
Try ProcessStartInfo. This will hide the console app, but I'm not positive about it hiding IE.
ProcessStartInfo psi = new ProcessStartInfo("iexplore.exe", #"http://myserver.com");
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(psi);

c# console application interacting with PHP page

I have a c# console application that I invoke with a server call in PHP
chdir($filePath);
exec($filePath.$fileName);
This works great, the application is run. The data it is designed to collect is collected and everyone is happy.
Currently I have plans on storing the one time use information on a server or a flat file, but then I noticed that while the console application is running and doing it's magic the page hangs waiting for the application to stop. This intrigued me, and now i'm wondering if there is a way for the application to pass it's data back to the page directly?
Note: I'm running Apache2 on Windows 7
Update:
Ended up using
$runCommand = "D:\\ScanBoyConsole\\ScanBoy_Console.exe COM1 9600 8 1 0 1";
$WshShell = new COM("WScript.Shell");
$output = $WshShell->Exec($runCommand)->StdOut->ReadAll;
json_decode($output);
If you mean by "directly" that you want the application's output sent to the client while it's still running you might be interested in passthru().
If you're also the author of the C# application you could skip the console application and expose the functionality in a way accessible via php's COM and .Net module.
The console app should be able to print (using Console.WriteLine), and PHP can take the results of that...
Back in my PHP days, we called scripts all the time (that are nothing but console apps basically) and had the results spit out to the page.
"shell_exec — Execute command via shell and return the complete output
as a string"
http://php.net/manual/en/function.shell-exec.php
[So, the only difference is that you should use shell_exec instead of a regular exec]

c# interactiveProcessRunner

Can someone tell me what the InteractiveProcessRunner is for? Is it identical to Process.Start?
Here is the class.
And here an example :
InteractiveProcessRunner runner =
new InteractiveProcessRunner(notepad.exe,hSessionToken);
THX
Whit this class you can run a process with the complete environment of the user active: if you call this code from a service, you should find the user mapped resources, the desktop and all the resources that are available when the user is loggen on interactively even if launched from a service ie not logged interactively.
The source code to which your link leads referes to this article: http://asprosys.blogspot.com/2009/03/perils-and-pitfalls-of-launching.html which explains the motivation behind it.
Summary: You can't really use Process.Start() when you want to start a new process as certain user from a windows service. The InteractiveProcessRunner lets you do this (supposedly, never used it so I can't verify it). So it's not the same as Process.Start() - it uses a different Windows API.

Categories

Resources