【发布时间】: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