Reputation: 75
I'm currently trying to have a simple CMD function run from my C# WPF program, and display the output in the WPF program. I think I'm close, but its not currently displaying anything. The program will open and have a confirmation that it should run the CMD file. Once 'Yes' is selected, it should open a new frame and run the program, piping it back to the frame. The CMD referenced is a simple netstat cmd. I have changed 'CreateNoWindow' to False and see the CMD opening, so it seems to be executing it.
EDIT: I left out a section of my code, whoops!
EDIT 2: Updated code to include some suggestions. No change. Could it be something to do with how I'm using a Frame?
namespace WpfApp1
{
/// <summary>
/// Interaction logic for Page3.xaml
/// </summary>
public partial class Page3 : Page
{
public Page3()
{
InitializeComponent();
}
private void Frame_Navigated(object sender, NavigationEventArgs e)
{
Task.Run(() => { Muntrainout(); });
}
public void Muntrainout()
{
System.Diagnostics.Process muntrainout = new System.Diagnostics.Process();
muntrainout.StartInfo.RedirectStandardOutput = true;
muntrainout.StartInfo.RedirectStandardError = true;
muntrainout.StartInfo.RedirectStandardInput = true;
muntrainout.StartInfo.UseShellExecute = false;
muntrainout.StartInfo.CreateNoWindow = true;
muntrainout.StartInfo.FileName = @"C:\Users\user.name\Documents\Project\Test.cmd";
muntrainout.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) =>
{
Console.WriteLine(e.Data);
}
);
// Error Handling
muntrainout.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) => { Console.WriteLine(e.Data); });
muntrainout.Start();
muntrainout.BeginOutputReadLine();
muntrainout.WaitForExit();
}
}
}
Upvotes: 2
Views: 1351
Reputation: 605
Assuming you want to display the output in your frame
...
this works for me:
muntrainout.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler((sender, e) =>
{
//Console.WriteLine(e.Data);
Frame1.Dispatcher.Invoke(() => { Frame1.Content += e.Data+Environment.NewLine; });
}
It looks a bit ugly but it displays the output... I don't know how Console.WriteLine()
should write to your frame?
Upvotes: 1