1

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

  • 1
    See the methods (and the notes) shown here: [How do I get output from a command to appear in a control on a Form in real-time?](https://stackoverflow.com/a/51682585/7444103). In your case, set `Arguments = "START /WAIT /K ipconfig /all"`. Don't use `IProgress` here, it's not needed: see the `SinchronizingObject` property (use the Form, not the TexBox/RichTextBox) – Jimi Aug 15 '20 at 00:49

0 Answers0