简体   繁体   English

如何在C#for .NET中从Windows窗体应用程序发送命令提示符参数?

[英]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. 用户单击标题为“ Ping”的按钮,然后使用命令提示符“ ping”对服务器执行ping操作。 The output is captured in a string and displayed in the windows forms app to the user. 输出以字符串形式捕获,并在Windows窗体应用程序中显示给用户。

Use the Process class to start a new process. 使用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. 您可以重定向标准错误和输出-在这种情况下,您可能希望以事件驱动的方式进行重定向,以便在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. 假设您想让用户看到输出,就不要在输出上使用ReadToEnd -在输出完成之前,它会阻塞。

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: 出于良好的考虑,这里有一个完整的WinForms应用程序:

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. 字符串变量sRes应该包含ping命令的结果。

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. 仅对Winforms有一个非常基本的了解,您可以自己填写空白。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用命令行参数从批处理文件运行Windows Forms Application(C#.NET 4.0) - Running Windows Forms Application (C# .NET 4.0) from batch file with Command Line Arguments 如何将控制台应用程序中的变量值传递给 C# 中的 windows 表单应用程序(Visual Studio) - How do i pass a variable value from Console application to windows form application in C# (visual studio) 如何从我的 C# windows 应用程序发送 email? - How do I send an email from my C# windows application? C# 如何为 Windows 窗体应用程序创建安装? - C# How do I create an installation for a windows form application? 如何从Windows Form应用程序将ac#变量传递给python脚本 - How do I pass a c# variable to a python script from windows form application 如何从C#Windows窗体应用程序的数据库中检索信息 - How do I retrieve information from database for my C# Windows Form Application 将字符串从 c# windows 表单应用程序发送到 asp.net Z2567A5EC9705EB7AC2C984033E06 服务器 - send string from c# windows form application to asp.net web server 使用SendMessage从Windows窗体应用程序将字符串发送到命令提示符(cmd) - Send String to Command Prompt (cmd) from Windows Form Application using SendMessage 如何从 C# 应用程序发送签名的电子邮件? - How do I send signed emails from C# application? 如何使用开发人员命令提示符更新 C#? - How do I update C# using developer command prompt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM