【问题标题】:Configuring network access in C#?在 C# 中配置网络访问?
【发布时间】:2014-08-08 18:35:56
【问题描述】:

我正在创建一个小型 Windows 窗体应用程序,允许我输入计算机名称或 IP 地址,然后单击各种按钮,这些按钮将调用命令窗口并执行各种任务,例如 ping、nslookup 等。

我遇到的问题是,当我尝试此操作时,例如 ping 功能就像我没有网络连接一样(“一般故障”)。如果我手动运行命令提示符并运行 ping 它工作正常。我是否必须以某种方式允许我的程序访问网络?如果是这样,我将如何处理?我使用适用于 Windows 桌面的 Visual Studio Express 2013。

谢谢

(注意:字符串“inputText”是从我的 GUI 上的文本框中捕获的字符串

//pings the computer or IP address received in the input box
    private void buttonPing_Click(object sender, EventArgs e)
    {

        bool noInput = String.IsNullOrEmpty(inputText);

        //refuses input if text box is empty
        if (noInput == true)
        {
            MessageBox.Show("Please enter a computer name or IP address in the text box.");
        }
        else
        {
            //starts an instance of the command prompt, and passes the string to the console
            String strCmdPing = String.Format("ping {0} -t", inputText);
            Process.Start("CMD", "/c" + strCmdPing);
        }
    }

【问题讨论】:

  • 看看你传递给CMD的参数
  • 你的代码对我来说很好。

标签: c# networking visual-studio-express


【解决方案1】:

MSDN

你可以简单地做这样的事情:

using System.Diagnostic;

protected void btnPing_Click(object sender, EventArgs e)
{
     if(string.IsNullOrEmpty(txtIPAddress.Text))
          throw new ArgumentNullException();

     string command = @"ping " + txtIPAddress.Text;
     Process.Start("CMD.exe", command);
}

这是一个简单的原始示例,但没有代码或更多信息,我相信这将是基本假设。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 2022-10-16
    • 2015-02-22
    • 2018-02-06
    • 2010-11-18
    • 2015-04-23
    • 1970-01-01
    相关资源
    最近更新 更多