Return 来自 catch System.Net.Sockets.SocketException 的字符串
Return string from catch System.Net.Sockets.SocketException
我有一个按钮可以将主机名列表转换为文本框中匹配的 IP 地址。我怎么能只 return 字符串 "No host is known" 作为文本框真的不知道的主机名?
我使用 try-catch 块来捕获 System.Net.Sockets.SocketException
。但据我所知,catch 块不能 return 任何字符串值。所以,我通常通过输出一个消息框来捕获异常。但是这一次,我只是想让它在指定的文本框中显示一个字符串。这是我试过的代码:-
private void btnConvertHosttoIP_Click(object sender, Eventrgs e)
{
try
{
string ips = null;
List<string> ipList = new List<string>();
string[] hostList = Regex.Split(txtHost.Text, "\r\n");
foreach (var h in hostList)
{
// Check DNS
if (h.Contains(".xxxx.com"))
{
hostName = h;
}
else
{
string code = txtHost.Text.Substring(0, 3);
if (code == "ABC" || code == "CDE")
hostName = h + ".ap.xxx.com";
else
hostName = "Unknown domain name";
}
IPHostEntry host = Dns.GetHostEntry(hostName);
IPAddress[] ipaddr = host.AddressList;
// Loop through the IP Address array and add the IP address to Listbox
foreach (IPAddress addr in ipaddr)
{
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipList.Add(addr.ToString());
}
}
}
foreach (var ip in ipList)
{
ips += ip + Environment.NewLine;
}
txtIP.Text = ips;
}
catch (System.Net.Sockets.SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
我希望未知主机仅作为例外显示在文本框中。是否可行或有其他建议?
您可以按如下方式修改您的代码。
try
{
string ips = null;
List<string> ipList = new List<string>();
string[] hostList = Regex.Split(txtHost.Text, "\r\n");
foreach (var h in hostList)
{
// Check DNS
if (h.Contains(".xxxx.com"))
{
hostName = h;
}
else
{
string code = txtHost.Text.Substring(0, 3);
if (code == "ABC" || code == "CDE")
hostName = h + ".ap.xxx.com";
else
hostName = "Unknown domain name";
}
try
{
IPHostEntry host = Dns.GetHostEntry(hostName);
IPAddress[] ipaddr = host.AddressList;
// Loop through the IP Address array and add the IP address to Listbox
foreach (IPAddress addr in ipaddr)
{
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipList.Add(addr.ToString());
}
}
}
catch (System.Net.Sockets.SocketException ex)
{
ipList.Add("Invalid Host");
}
}
foreach (var ip in ipList)
{
ips += ip + Environment.NewLine;
}
txtIP.Text = ips;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我有一个按钮可以将主机名列表转换为文本框中匹配的 IP 地址。我怎么能只 return 字符串 "No host is known" 作为文本框真的不知道的主机名?
我使用 try-catch 块来捕获 System.Net.Sockets.SocketException
。但据我所知,catch 块不能 return 任何字符串值。所以,我通常通过输出一个消息框来捕获异常。但是这一次,我只是想让它在指定的文本框中显示一个字符串。这是我试过的代码:-
private void btnConvertHosttoIP_Click(object sender, Eventrgs e)
{
try
{
string ips = null;
List<string> ipList = new List<string>();
string[] hostList = Regex.Split(txtHost.Text, "\r\n");
foreach (var h in hostList)
{
// Check DNS
if (h.Contains(".xxxx.com"))
{
hostName = h;
}
else
{
string code = txtHost.Text.Substring(0, 3);
if (code == "ABC" || code == "CDE")
hostName = h + ".ap.xxx.com";
else
hostName = "Unknown domain name";
}
IPHostEntry host = Dns.GetHostEntry(hostName);
IPAddress[] ipaddr = host.AddressList;
// Loop through the IP Address array and add the IP address to Listbox
foreach (IPAddress addr in ipaddr)
{
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipList.Add(addr.ToString());
}
}
}
foreach (var ip in ipList)
{
ips += ip + Environment.NewLine;
}
txtIP.Text = ips;
}
catch (System.Net.Sockets.SocketException ex)
{
MessageBox.Show(ex.Message);
}
}
我希望未知主机仅作为例外显示在文本框中。是否可行或有其他建议?
您可以按如下方式修改您的代码。
try
{
string ips = null;
List<string> ipList = new List<string>();
string[] hostList = Regex.Split(txtHost.Text, "\r\n");
foreach (var h in hostList)
{
// Check DNS
if (h.Contains(".xxxx.com"))
{
hostName = h;
}
else
{
string code = txtHost.Text.Substring(0, 3);
if (code == "ABC" || code == "CDE")
hostName = h + ".ap.xxx.com";
else
hostName = "Unknown domain name";
}
try
{
IPHostEntry host = Dns.GetHostEntry(hostName);
IPAddress[] ipaddr = host.AddressList;
// Loop through the IP Address array and add the IP address to Listbox
foreach (IPAddress addr in ipaddr)
{
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipList.Add(addr.ToString());
}
}
}
catch (System.Net.Sockets.SocketException ex)
{
ipList.Add("Invalid Host");
}
}
foreach (var ip in ipList)
{
ips += ip + Environment.NewLine;
}
txtIP.Text = ips;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}