简体   繁体   中英

How Do I Send Command Prompt Arguments from a Windows Form Application in C# for .NET?

This one should be an easy one.

An example would be the following:

The user clicks a button titled "Ping" and it uses the command prompt "ping" to ping the server. The output is captured in a string and displayed in the windows forms app to the user.

Use the Process class to start a new process. You can redirect standard error and output - in this case you probably want to do so in an event-driven manner, to write text to the app when it comes out of ping.

Assuming you want the user to see output as it comes, you don't want to use ReadToEnd on the output - that will block until it's finished.

Here's a complete example which writes to the console - but it shows how to do it as the process writes output, rather than waiting for the process to finish:

using System;
using System.Diagnostics;

class Test
{
    static void Main(string[] args)
    {
        ProcessStartInfo psi = new ProcessStartInfo("ping")
        {
            UseShellExecute = false,
            RedirectStandardError = true,
            RedirectStandardOutput = true,
            Arguments = "google.com"
        };
        Process proc = Process.Start(psi);
        proc.EnableRaisingEvents = true;
        proc.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
        proc.BeginOutputReadLine();
        proc.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data);
        proc.BeginErrorReadLine();
        proc.WaitForExit();
        Console.WriteLine("Done");
    }
}

Just for good measure, here's a complete WinForms app:

using System;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

class Test
{
    [STAThread]
    static void Main(string[] args)
    {
        Button button = new Button {
            Text = "Click me",
            Dock = DockStyle.Top
        };        
        TextBox textBox = new TextBox {
            Multiline = true,
            ReadOnly = true,
            Location = new Point(5, 50),
            Dock = DockStyle.Fill
        };
        Form form = new Form {
            Text = "Pinger",
            Size = new Size(500, 300),
            Controls = { textBox, button }
        };

        Action<string> appendLine = line => {
            MethodInvoker invoker = () => textBox.AppendText(line + "\r\n");
            textBox.BeginInvoke(invoker);
        };

        button.Click += delegate
        {        
            ProcessStartInfo psi = new ProcessStartInfo("ping")
            {
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                Arguments = "google.com"
            };
            Process proc = Process.Start(psi);
            proc.EnableRaisingEvents = true;
            proc.OutputDataReceived += (s, e) => appendLine(e.Data);
            proc.BeginOutputReadLine();
            proc.ErrorDataReceived += (s, e) => appendLine(e.Data);
            proc.BeginErrorReadLine();
            proc.Exited += delegate { appendLine("Done"); };
        };

        Application.Run(form);
    }
}
Process p = System.Diagnostics.Process.Start("ping ...."); 
System.IO.StreamReader reader = p.StandardOutput; 
string sRes = reader.ReadToEnd(); 
reader.Close(); 

The string variable sRes should contain the results of the ping command.

public static string Ping(string url)
{
    Process p = System.Diagnostics.Process.Start("ping.exe", url);
    StreamReader reader = p.StandardOutput;

    String output = reader.ReadToEnd();
    reader.Close();

    return output;
}

I covered this pretty extensively on my blog . You can copy paste the fully compileable example. With only a very basic understanding of Winforms you can fill in the blanks yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM