这是我的ftp:
ftp://192.168.2.4我想要192.168.2.4
我试过的
string ipAddress = FTPAddress.Substring(5,11)这是可行的
问题所在
正如您所看到的,我将长度设置为11,但是,当我更改ftp地址时,11将不能工作,我需要新的ftp地址长度。你能帮帮忙吗
可能是反流表达?
更新
有时ftp地址可能如下所示:ftp://ip/folder
发布于 2014-12-24 10:25:06
创建一个Uri对象。IP地址将位于Host属性中。
Uri link = new Uri("ftp://192.168.2.4");
string IpAddress = link.Host;Uri类还可以提供更多的有用信息。
发布于 2014-12-24 10:25:11
您可以使用URI类来处理地址,然后使用读取其中的部分。
Uri ftpAddress = new Uri("ftp://192.168.2.4");
string ipAddress = ftpAddress.Host;如果需要将主机名转换为IPAddresses,下面的扩展类可能会有所帮助(基于这个回答):
public static class UriHostResolveExtension
{
public static String ResolveHostnameToIp(this Uri uri)
{
if (uri.HostNameType == UriHostNameType.Dns)
{
IPHostEntry hostEntry = Dns.GetHostEntry(uri.Host);
if (hostEntry.AddressList.Length > 0)
return hostEntry.AddressList[0].ToString();
}
return uri.Host;
}
}可以在以下场景中使用:
Uri ftpAddress = new Uri("ftp://example.com/");
string ipAddress = ftpAddress.ResolveHostnameToIp();https://stackoverflow.com/questions/27635303
复制相似问题