ping.send 字符串参数不起作用
ping.send by string parameter not working
我最近在 C# Winform 项目中开发一个函数,检查 IP 连接状态(工作正常,因为我已经对其进行了测试),这是我的代码。
public static bool checkConnection()
{
Ping pinger = new Ping();
try
{
return pinger.Send("192.168.0.2").Status == IPStatus.Success;
}
catch
{
Console.WriteLine("connection fail");
return false;
}
}
但是当我尝试替换 pinger.Send("192.168.0.2").Status == IPStatus.Success;
使用以下代码
String router_IP = "192.168.0.2";
return pinger.Send(router_IP).Status == IPStatus.Success;
编译器不会接受这种用法.....
然后,我也试了下面的代码,也不行。
IPAddress ip_address = IPAddress.Parse(router_IP);
return pinger.Send(ip_address).Status == IPStatus.Success;
所以,我的问题是:
有谁知道如何在 pinger.Send
中解析 字符串变量 而不仅仅是发送“192.168.0.2”?
这是我的Visual Studio 2017的图片(抱歉有中文说法,我会翻译VS 2017提供的建议)
The error message I got from VS 2017
错误消息显示:"It needs object to look up reference, so it can use non-static method or properties "FileUpload.router_IP"
据我所知,您只是缺少 static
关键字。
您的代码如下所示:
string router_IP = "192.168.0.2";
public static bool checkConnection()
{
Ping pinger = new Ping();
try
{
return pinger.Send(router_IP).Status == IPStatus.Success;
}
catch
{
Console.WriteLine("connection fail");
return false;
}
}
将第一行改为:
static string router_IP = "192.168.0.2";
请注意,它现在带有前缀 static
。
现在去阅读 the static keyword in C#
或者,您可以更改方法以接受 IP 地址的 string
表示形式作为参数:
public static bool checkConnection(string router_IP)
{
Ping pinger = new Ping();
try
{
return pinger.Send(router_IP).Status == IPStatus.Success;
}
catch
{
Console.WriteLine("connection fail");
return false;
}
}
现在你可以这样称呼它:
var result = checkConnection("192.168.0.2");
我最近在 C# Winform 项目中开发一个函数,检查 IP 连接状态(工作正常,因为我已经对其进行了测试),这是我的代码。
public static bool checkConnection()
{
Ping pinger = new Ping();
try
{
return pinger.Send("192.168.0.2").Status == IPStatus.Success;
}
catch
{
Console.WriteLine("connection fail");
return false;
}
}
但是当我尝试替换 pinger.Send("192.168.0.2").Status == IPStatus.Success;
使用以下代码
String router_IP = "192.168.0.2";
return pinger.Send(router_IP).Status == IPStatus.Success;
编译器不会接受这种用法.....
然后,我也试了下面的代码,也不行。
IPAddress ip_address = IPAddress.Parse(router_IP);
return pinger.Send(ip_address).Status == IPStatus.Success;
所以,我的问题是:
有谁知道如何在 pinger.Send
中解析 字符串变量 而不仅仅是发送“192.168.0.2”?
这是我的Visual Studio 2017的图片(抱歉有中文说法,我会翻译VS 2017提供的建议)
The error message I got from VS 2017 错误消息显示:"It needs object to look up reference, so it can use non-static method or properties "FileUpload.router_IP"
据我所知,您只是缺少 static
关键字。
您的代码如下所示:
string router_IP = "192.168.0.2";
public static bool checkConnection()
{
Ping pinger = new Ping();
try
{
return pinger.Send(router_IP).Status == IPStatus.Success;
}
catch
{
Console.WriteLine("connection fail");
return false;
}
}
将第一行改为:
static string router_IP = "192.168.0.2";
请注意,它现在带有前缀 static
。
现在去阅读 the static keyword in C#
或者,您可以更改方法以接受 IP 地址的 string
表示形式作为参数:
public static bool checkConnection(string router_IP)
{
Ping pinger = new Ping();
try
{
return pinger.Send(router_IP).Status == IPStatus.Success;
}
catch
{
Console.WriteLine("connection fail");
return false;
}
}
现在你可以这样称呼它:
var result = checkConnection("192.168.0.2");