我正在使用Ping
中的System.Net.NetworkInformation
类,我需要将一些字符串数据发送到PingCompleted
事件处理程序的事件函数。
我试过这个:
void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e, string adress)
{
}
...
ex_ping.PingCompleted += (sender1, args) => ex_ping_PingCompleted(sender1, new
PingCompletedEventArgs(), adress);
但它告诉我PingCompletedEventArgs
没有构造函数。
我试图制作自己的事件args:
public class ProgressEventArgs : PingCompletedEventArgs
{
public string adress;
public ProgressEventArgs(string ex_adress)
{
adress = ex_adress;
}
}
这里的错误相同。
我只是想知道我所说的地址,事件函数中的e.Reply.Address
给了我IP,我需要网站名称。
编辑: 我自己想出来了:
ex_ping.PingCompleted += (sender1, args) => ex_ping_PingCompleted(sender1, args,adress);
void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e,string ex_adress) {
}
工作正常,/ close
答案 0 :(得分:1)
订阅活动
ex_ping.PingCompleted += ex_ping_PingCompleted;
在处理程序中获取地址
void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
// verify if operation was not canceled or some error occured
var address = e.Reply.Address;
IPHostEntry entry = Dns.GetHostEntry(address);
var siteName = entry.HostName;
}
答案 1 :(得分:0)
你在UserState
财产之后:
void Ping()
{
string address = "google.com";
ex_ping.PingCompleted+=ex_ping_PingCompleted;
ex_ping.SendAsync(address, 500, buffer, options, address);
}
void ex_ping_PingCompleted(object sender, PingCompletedEventArgs e)
{
// Will give you the hostname
var address = e.UserState;
}