There is a NewClass.cs
in which I collect data about ipconfig
from cmd. How can I output data from this class (via arguments) to the TextBox
that lies on the form ?!
public static class NewClass
{
public static void Test()
{
ProcessStartInfo Info = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "ipconfig /all",
CreateNoWindow = false,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Normal
};
Process result = Process.Start(Info);
result.WaitForExit();
}
}
On the form lies a TextBox, you need to make a call to the Test method (with passing arguments) from the NewClass.cs
class so that everything from the command line (cmd.exe) is displayed in this TextBox
without the console appearing.
How to implement this? \
I tried like this but it doesn't work\
private void button1_click(object sender, EventArgs e)
{
IProgress<string> progress = new Progress<string>(s =>
{
textBox1.Text += s;
});
NewClass.Test(progress);
}
public static class NewClass
{
public static void Test(IProgress<string> progress)
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "ipconfig /all";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(866);
process.StartInfo.StandardErrorEncoding = Encoding.GetEncoding(866);
var handler = new DataReceivedEventHandler((s, e) => progress?.Report(e.Data));
process.OutputDataReceived += handler;
process.ErrorDataReceived += handler;
process.Start();
process.BeginOutputReadLine();
}
}
Help Please