参数按照ASCII码从小到大排序,获取本机ip
作者:互联网
1 /// <summary> 2 /// 参数按照ASCII码从小到大排序 3 /// </summary> 4 /// <param name="paramsMap"></param> 5 /// <returns></returns> 6 private string getParamSrc(Dictionary<string, string> paramsMap) 7 { 8 var vDic = (from objDic in paramsMap orderby objDic.Key ascending select objDic); 9 StringBuilder str = new StringBuilder(); 10 foreach (KeyValuePair<string, string> kv in vDic) 11 { 12 string pkey = kv.Key; 13 string pvalue = kv.Value; 14 str.Append(pkey + "=" + pvalue + "&"); 15 } 16 17 string result = str.ToString().Substring(0, str.ToString().Length - 1); 18 return result; 19 }
1 /// <summary> 2 /// 获取本机ip 3 /// </summary> 4 /// <returns></returns> 5 private string GetLocalIP() 6 { 7 try 8 { 9 string HostName = Dns.GetHostName(); //得到主机名 10 IPHostEntry IpEntry = Dns.GetHostEntry(HostName); 11 for (int i = 0; i < IpEntry.AddressList.Length; i++) 12 { 13 //从IP地址列表中筛选出IPv4类型的IP地址 14 //AddressFamily.InterNetwork表示此IP为IPv4, 15 //AddressFamily.InterNetworkV6表示此地址为IPv6类型 16 if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork) 17 { 18 return IpEntry.AddressList[i].ToString(); 19 } 20 } 21 LogInfo("未能获取到本机IP"); 22 return ""; 23 } 24 catch (Exception ex) 25 { 26 LogInfo("获取本机IP出错:" + ex.Message); 27 return ""; 28 } 29 }
标签:return,string,ip,IpEntry,str,本机,ASCII,AddressFamily 来源: https://www.cnblogs.com/smiledGo/p/11611503.html