I have a console application which does a set of operations and gives out messages after completion of each operation. When I run my console app, the messages in my console window may look like this:
Checking prerequisites...
Completing prerequisites..
Performing installation...
Completing installation...
Done..!
Now I'm executing this console application from one of my C# windows applications by using Process.StartInfo().
I need to get all the messages thrown by my console application to be displayed in the windows form of my application.
Can this be done?
Thanks.
Look here Capturing console output from a .NET application (C#)
This can be quite easily achieved using the ProcessStartInfo.RedirectStandardOutput Property. A full sample is contained in the linked MSDN documentation.
Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();
Console.WriteLine(compiler.StandardOutput.ReadToEnd());
compiler.WaitForExit();
Related
Now I want to launch a Windows 8 Metro App by a C# form application, but it's hard for me to find the right way...
I had tried these ways(I had installed the Metro App and registered a protocol):
1.
var success = await Windows.System.Launcher.LaunchUriAsync(new Uri("TestContent"));
In the "TestContent" above, I tried the protocol name registered on my computer and the Metro App's name, but the same error "Access Denied" occurred.
2.
Process p = new Process();
p.StartInfo.FileName = "ProtocolName://";
p.Start();
This time the Metro App only showed the splash screen but didn't launch the whole program.
Any ideas?
Thank you in advance.
2015/10/01 ADD:
After Reading this question:
[link]stackoverflow.com/questions/18787609/…
I think it is impossible to run a metro app from a windows form.
The right format to launch is :
Uri uri = new Uri("protocolname://anything");
await Windows.System.Launcher.LaunchUriAsync(uri);
And, Make sure that you're doing that from the foreground app, i.e. Not Background Task or a Toast notification or Tile .. etc.
After Reading this question:
C# Mixing WinRT and Windows Forms
I think it may be impossible to run a metro app from a windows form.
But when I try the code below :
Process p = new Process();
p.StartInfo.FileName = #"BINGMAPS://";
p.Start();
The BINGMAPS runs normally.
Did I miss any necessary setting in the Metro App?
First you need your app to handle protocol activation and then in App.xaml.cs in OnActivated event handle it to launch the first page or whatever page you want to call in Metro app. In C# form application using javascript set the current page content as the metro app. It will work.
I need to launch or run an windows exe file from a OEM in Win store App.
can below be used? I try but have error but why since diagnostic is in the framework
using System.Diagnostics;
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
// here I point to the OEM windows exe file
myProcess.StartInfo.FileName = "C:\\HelloWorld.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
-- Update
# Michal :
Thank you. This method works using URI.
One problem :
the WinRT app goes into background after launching. How to make the winRT app not going background?
Is there any other way? I need to integrate the Exe from the OEM into Win Store App.
Note:
I want to know is this can be done or I am doing something which is not allowed?
If I didnot make my case clearer, please tell me what I miss.
I tried it but it did not work. So my solution should be using similar approach like Diagnostic but what it can be?
Recycling bits from https://social.msdn.microsoft.com/Forums/windowsapps/en-US/0a822355-909f-44b4-9c79-cb986087fe27/after-launching-or-activate-an-app-the-main-app-goes-into-background?forum=winappswithcsharp
This is expected behavior and there's no good way around it. Launching a file or protocol switches to the default handler with no expectation of return.
Launching an app via protocol like this a hack in the first place. Since you are a side-loaded app look into writing a Brokered Windows Runtime Component to allow proper use of desktop API and communication with a desktop back-end.
See the Brokered Windows Runtime Component docs at http://msdn.microsoft.com/en-us/library/windows/apps/dn630195.aspx
When you launch a console application by double-clicking it in explorer, the console is visible. However, if you run a console application through an already non-visible application, the console is invisible (maybe because it is trying to read the standard output or something).
So, how can I show my console window to the user even if the console application was started by a background app?
I already tried the ShowWindow and SetForeground Windows API without any luck. I also tried the following.
//paths for starting the server.
var processInformation = new ProcessStartInfo(myOwnPath);
processInformation.WorkingDirectory = myOwnWorkingDirectory;
processInformation.UseShellExecute = false;
processInformation.WindowStyle = ProcessWindowStyle.Normal;
processInformation.CreateNoWindow = false;
processInformation.RedirectStandardInput = false;
processInformation.RedirectStandardError = false;
processInformation.RedirectStandardOutput = false;
Process.Start(processInformation);
Any ideas?
Note: It is perfectly fine if the application needs to launch itself and shut down the old instance to get the console window visible.
Try setting UseShellExecute to true. Setting it to false lets you to redirect input, output, and error streams.
I am trying to input console commands on a desktop app (through a text box) click a button, execute the command and get the returned values back for display.
The point is, I want to make my work faster and less stressful when trying to input repetitive commands on the console over and over again everyday.
I know some console application programming on C# and web applications(.NET, ADO.NET) on C# as well, but nothing about desktop applications. Any ideas?
The Process class can be used to start an application, and read it's output (from the standard output or standard error streams).
For example, to start a process, and read it's "output", you could use:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Foo.exe";
p.Start();
// Read the output
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
If you make a basic windows forms applications, you could store repetitive commands in a local file and have a mechanism to choose a command from this file in your application and run it. Windows forms shouldn't be too hard for you. it is just drag and drop.
Is it possible to embed a DOS console in a Windows Form or User Control in C# 2.0?
We have a legacy DOS product that my Windows app has to interact with, and it's been requested that an instance of the legacy product should run within the Windows application.
At the moment, I'm using the user32.dll to locate the window that the DOS product is running in, minimising then maximising the window, and typing characters into the window. This isn't a very good solution to the problem, as it means my application has to store the window name in application settings, and requires that the user returns to the correct page of the DOS app before using the interaction function.
EDIT: Some more information
The legacy app needs to be visible to the user, but not in a separate window.
I've tried TimothyP's answer and it works very well, but is it possible to achieve the same functionality, but with the DOS window visually embedded in a windows form or user control instead of popping up in it's own window? Preferably in a ShowDialog() way so that the user cannot interact with the app wile they are in 'Legacy Mode', so to speak.
It's possible to redirect the standard input/output of console/dos applications using the Process class. It might look something like this:
var processStartInfo = new ProcessStartInfo("someoldapp.exe", "-p someparameters");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();
You can now use the streams to interact with the application.
By setting processStartInfo.CreateNoWindow to true the original application will be hidden.
I hope this helps.
Concerning your question on how to display the DOS app inside the Windows app.
There are a few solutions.
The first one is to simply not display the DOS app (with CreateNoWindow)
and "simulate" the UI of the DOS app in your Windows application by reading and writing to the streams.
The other solution would be to use the Win32API, get the Windows Handle (Whnd) of the Console/DOS application window and set its parent to your form. I'm currently not at home
and since it has been ages since I've done this, I can't remember by heart how it is done. If I'm not mistaken you'll need to use the following Win32 API calls:
FindWindow
GetWindow
SetParent
If I have some time left later today, I'll see if I can find better samples.
You can use the CreateProcess function and the hStdInput, Output, and Error members of the STARTUPINFO argument, this will allow you to intercept standard input and output of the application.