网络(运营商信息,网络类型)

  1. public static class NetworkInfo
  2. {
  3. /// <summary>
  4. /// 网络是否可用
  5. /// </summary>
  6. public static bool IsNetworkAvailable
  7. {
  8. get
  9. {
  10. ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
  11. return (profile?.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
  12. }
  13. }
  14.  
  15. /// <summary>
  16. /// 获取IP地址
  17. /// </summary>
  18. /// <returns>IP地址</returns>
  19. public static string GetIpAddress()
  20. {
  21. Guid? networkAdapterId = NetworkInformation.GetInternetConnectionProfile()?.NetworkAdapter?.NetworkAdapterId;
  22. return (networkAdapterId.HasValue ? NetworkInformation.GetHostNames().FirstOrDefault(hn => hn?.IPInformation?.NetworkAdapter.NetworkAdapterId == networkAdapterId)?.CanonicalName : null);
  23. }
  24.  
  25. /// <summary>
  26. /// 获取网络运营商信息
  27. /// </summary>
  28. /// <returns></returns>
  29. public static string GetNetworkName()
  30. {
  31. try
  32. {
  33. ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
  34. if (profile != null)
  35. {
  36. if (profile.IsWwanConnectionProfile)
  37. {
  38. var homeProviderId = profile.WwanConnectionProfileDetails.HomeProviderId;
  39. //4600是我手机测试出来的。
  40. if (homeProviderId == "" || homeProviderId == "" || homeProviderId == "")
  41. {
  42. return "中国移动";
  43. }
  44. //已验证
  45. else if (homeProviderId == "")
  46. {
  47. return "中国联通";
  48. }
  49. //貌似还没win10 电信手机。。待验证
  50. else if (homeProviderId == "")
  51. {
  52. return "中国电信";
  53. }
  54. }
  55. else
  56. {
  57. return "其他";
  58. }
  59. //也可以用下面的方法,已验证移动和联通
  60. //var name = profile.GetNetworkNames().FirstOrDefault();
  61. //if (name != null)
  62. //{
  63. // name = name.ToUpper();
  64. // if (name == "CMCC")
  65. // {
  66. // return "中国移动";
  67. // }
  68. // else if (name == "UNICOM")
  69. // {
  70. // return "中国联通";
  71. // }
  72. // else if (name == "TELECOM")
  73. // {
  74. // return "中国电信";
  75. // }
  76. //}
  77. //return "其他";
  78. }
  79.  
  80. return "其他";
  81. }
  82. catch (Exception)
  83. {
  84.  
  85. return "其他";
  86. }
  87.  
  88. }
  89.  
  90. /// <summary>
  91. /// 获取网络连接类型
  92. /// </summary>
  93. /// <returns></returns>
  94. public static string GetNetWorkType()
  95. {
  96. try
  97. {
  98. ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
  99. if (profile == null)
  100. {
  101. return "未知";
  102. }
  103. if (profile.IsWwanConnectionProfile)
  104. {
  105. WwanDataClass connectionClass = profile.WwanConnectionProfileDetails.GetCurrentDataClass();
  106. switch (connectionClass)
  107. {
  108. //2G-equivalent
  109. case WwanDataClass.Edge:
  110. case WwanDataClass.Gprs:
  111. return "2G";
  112. //3G-equivalent
  113. case WwanDataClass.Cdma1xEvdo:
  114. case WwanDataClass.Cdma1xEvdoRevA:
  115. case WwanDataClass.Cdma1xEvdoRevB:
  116. case WwanDataClass.Cdma1xEvdv:
  117. case WwanDataClass.Cdma1xRtt:
  118. case WwanDataClass.Cdma3xRtt:
  119. case WwanDataClass.CdmaUmb:
  120. case WwanDataClass.Umts:
  121. case WwanDataClass.Hsdpa:
  122. case WwanDataClass.Hsupa:
  123. return "3G";
  124. //4G-equivalent
  125. case WwanDataClass.LteAdvanced:
  126. return "4G";
  127.  
  128. //not connected
  129. case WwanDataClass.None:
  130. return "未连接";
  131.  
  132. //unknown
  133. case WwanDataClass.Custom:
  134. default:
  135. return "未知";
  136. }
  137. }
  138. else if (profile.IsWlanConnectionProfile)
  139. {
  140. return "WIFI";
  141. }
  142. return "未知";
  143. }
  144. catch (Exception)
  145. {
  146. return "未知"; //as default
  147. }
  148.  
  149. }
  150. }

设备信息(分辨率,设备类型(PC,平板,手机,Xbox))

  1. /// <summary>
  2. /// 设备信息
  3. /// </summary>
  4. public static class DeviceInfo
  5. {
  6. /// <summary>
  7. /// 设备ID
  8. /// </summary>
  9. public static readonly string DeviceId;
  10.  
  11. /// <summary>
  12. /// 用户代理
  13. /// </summary>
  14. public static readonly string UserAgent;
  15.  
  16. /// <summary>
  17. /// 操作系统版本
  18. /// </summary>
  19. public static readonly string OsVersion;
  20.  
  21. /// <summary>
  22. /// 设备分辨率
  23. /// </summary>
  24. public static readonly Size DeviceResolution;
  25.  
  26. /// <summary>
  27. /// 设备时区名字
  28. /// </summary>
  29. public static readonly string Timezone;
  30.  
  31. /// <summary>
  32. /// 设备语言
  33. /// </summary>
  34. public static readonly string Language;
  35.  
  36. /// <summary>
  37. /// 设备类型
  38. /// </summary>
  39. public static readonly string DeviceType;
  40.  
  41. static DeviceInfo()
  42. {
  43. DeviceId = GetDeviceId();
  44. UserAgent = GetUserAgent();
  45. OsVersion = GetOsVersion();
  46. DeviceResolution = GetDeviceResolution();
  47. Timezone = GetTimezone();
  48. Language = GetLanguage();
  49. DeviceType = GetDeviceType();
  50. }
  51.  
  52. private static string GetDeviceType()
  53. {
  54. var deviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily;
  55.  
  56. if (deviceFamily == "Windows.Desktop")
  57. {
  58. if (UIViewSettings.GetForCurrentView().UserInteractionMode == UserInteractionMode.Mouse)
  59. {
  60. return "WINDESKTOP";
  61. }
  62. else
  63. {
  64. return "WINPAD";
  65. }
  66. }
  67. else if (deviceFamily == "Windows.Mobile")
  68. {
  69. return "WINPHONE";
  70. }
  71. else if (deviceFamily == "Windows.Xbox")
  72. {
  73. return "XBOX";
  74. }
  75. else if (deviceFamily == "Windows.IoT")
  76. {
  77. return "IOT";
  78. }
  79. else
  80. {
  81. return deviceFamily.ToUpper();
  82. }
  83. }
  84.  
  85. /// <summary>
  86. /// 获取设备语言
  87. /// </summary>
  88. /// <returns>设备语言</returns>
  89. private static string GetLanguage()
  90. {
  91. var Languages = Windows.System.UserProfile.GlobalizationPreferences.Languages;
  92. if (Languages.Count > )
  93. {
  94. return Languages[];
  95. }
  96. return Windows.Globalization.Language.CurrentInputMethodLanguageTag;
  97. }
  98.  
  99. /// <summary>
  100. /// 获取设备时区名字
  101. /// </summary>
  102. /// <returns>设备时区名字</returns>
  103. private static string GetTimezone()
  104. {
  105. return TimeZoneInfo.Local.DisplayName;
  106. }
  107.  
  108. /// <summary>
  109. /// 获取设备分辨率
  110. /// </summary>
  111. /// <returns>设备分辨率</returns>
  112. private static Size GetDeviceResolution()
  113. {
  114. Size resolution = Size.Empty;
  115. var rawPixelsPerViewPixel = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
  116. foreach (var item in PointerDevice.GetPointerDevices())
  117. {
  118. resolution.Width = item.ScreenRect.Width * rawPixelsPerViewPixel;
  119. resolution.Height = item.ScreenRect.Height * rawPixelsPerViewPixel;
  120. break;
  121. }
  122. return resolution;
  123. }
  124.  
  125. /// <summary>
  126. /// 获取设备ID
  127. /// </summary>
  128. /// <returns>设备ID</returns>
  129. private static string GetDeviceId()
  130. {
  131. HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
  132. return CryptographyHelper.Md5Encrypt(token.Id);
  133. }
  134.  
  135. /// <summary>
  136. /// 获取用户代理
  137. /// </summary>
  138. /// <returns>用户代理</returns>
  139. private static string GetUserAgent()
  140. {
  141. var Info = new EasClientDeviceInformation();
  142. return $"{Info.SystemManufacturer} {Info.SystemProductName}";
  143. }
  144.  
  145. /// <summary>
  146. /// 获取操作系统版本
  147. /// </summary>
  148. /// <returns>操作系统版本</returns>
  149. private static string GetOsVersion()
  150. {
  151. ulong version = Convert.ToUInt64(AnalyticsInfo.VersionInfo.DeviceFamilyVersion);
  152. return $"{version >> 48 & 0xFFFF}.{version >> 32 & 0xFFFF}.{version >> 16 & 0xFFFF}.{version & 0xFFFF}";
  153. }
  154.  
  155. }
  156.  
  157. /// <summary>
  158. /// 加密帮助类
  159. /// </summary>
  160. public static class CryptographyHelper
  161. {
  162. public static string DesEncrypt(string key, string plaintext)
  163. {
  164. SymmetricKeyAlgorithmProvider des = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.DesEcbPkcs7);
  165. IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
  166. CryptographicKey symmetricKey = des.CreateSymmetricKey(keyMaterial);
  167.  
  168. IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(plaintext, BinaryStringEncoding.Utf8);
  169.  
  170. IBuffer cipherBuffer = CryptographicEngine.Encrypt(symmetricKey, plainBuffer, null);
  171. return CryptographicBuffer.EncodeToHexString(cipherBuffer);
  172. }
  173.  
  174. public static string TripleDesDecrypt(string key, string ciphertext)
  175. {
  176. SymmetricKeyAlgorithmProvider tripleDes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcb);
  177. IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
  178. CryptographicKey symmetricKey = tripleDes.CreateSymmetricKey(keyMaterial);
  179.  
  180. IBuffer cipherBuffer = CryptographicBuffer.DecodeFromHexString(ciphertext);
  181.  
  182. IBuffer plainBuffer = CryptographicEngine.Decrypt(symmetricKey, cipherBuffer, null);
  183. return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, plainBuffer);
  184. }
  185.  
  186. public static string Md5Encrypt(string value)
  187. {
  188. IBuffer data = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
  189. return Md5Encrypt(data);
  190. }
  191.  
  192. public static string Md5Encrypt(IBuffer data)
  193. {
  194. HashAlgorithmProvider md5 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
  195. IBuffer hashedData = md5.HashData(data);
  196. return CryptographicBuffer.EncodeToHexString(hashedData);
  197. }
  198.  
  199. public static string EncodeToBase64String(string value)
  200. {
  201. IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
  202. return CryptographicBuffer.EncodeToBase64String(buffer);
  203. }
  204.  
  205. public static string DecodeFromBase64String(string value)
  206. {
  207. IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(value);
  208. return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffer);
  209. }
  210. }

整理UWP中网络和设备信息获取的帮助类,需要的拿走。的更多相关文章

  1. Centos7中网络及设备相关配置

    centos7中,不再赞成使用ifconfig工具,取而代之的是nmcli工具,服务管理也是以systemctl工具取代了service,这些之前版本的工具虽然在centos7中还可以继续使用,只是出 ...

  2. 获取设备信息——获取客户端ip地址和mac地址

    1.获取本地IP(有可能是 内网IP,192.168.xxx.xxx) /** * 获取本地IP * * @return */ public static String getLocalIpAddre ...

  3. 史上最全的iOS各种设备信息获取总结

    来源:si1ence 链接:http://www.jianshu.com/p/b23016bb97af 为了统计用户信息.下发广告,服务器端往往需要手机用户设备及app的各种信息,下面讲述一下各种信息 ...

  4. iOS: iOS各种设备信息获取

    Author:si1ence Link:http://www.jianshu.com/p/b23016bb97af 为了统计用户信息.下发广告,服务器端往往需要手机用户设备及app的各种信息,下面讲述 ...

  5. iOS 设备信息获取

    參考:http://blog.csdn.net/decajes/article/details/41807977參考:http://zengrong.net/post/2152.htm1. 获取设备的 ...

  6. Python 网络爬虫与信息获取(一)—— requests 库的网络爬虫

    1. 安装与测试 进入 cmd(以管理员权限),使用 pip 工具,pip install requests 进行安装: 基本用法: >> import requests >> ...

  7. 亚马逊商品页面的简单爬取 --Pyhon网络爬虫与信息获取

    1.亚马逊商品页面链接地址(本次要爬取的页面url) https://www.amazon.cn/dp/B07BSLQ65P/ 2.代码部分 import requestsurl = "ht ...

  8. 京东某商品页面的简单爬取 --Pyhon网络爬虫与信息获取

    1.京东商品页面链接地址(本次要爬取的页面url) https://item.jd.hk/1953999200.html 2.代码部分 import requestsurl = "https ...

  9. Python 网络爬虫与信息获取(二)—— 页面内容提取

    1. 获取超链接 python获取指定网页上所有超链接的方法 links = re.findall(b'"((http|ftp)s?://.*?)"', html) links = ...

随机推荐

  1. bzoj 1014 splay维护hash值

    被后缀三人组虐了一下午,写道水题愉悦身心. 题很裸,求lcq时二分下答案就行了,写的不优美会被卡时. (写题时精神恍惚,不知不觉写了快两百行...竟然调都没调就A了...我还是继续看后缀自动机吧... ...

  2. 做参数可以读取参数 保存参数 用xml文件的方式

    做参数可以读取参数 保存参数 用xml文件的方式 好处:供不同用户保存适合自己使用的参数

  3. elasticsearch snapshot

    一.Repositories 在elasticsearch.yml文件中增加path.repo路径配置: $ vim /etc/elasticsearch/elasticsearch.yml path ...

  4. jquery-easyui 树的使用笔记

    通常还是使用jquery-ui, 它是完全免费的, jquery-easyui可以使用 freeware edition. 但easyui还不是完全免费的: 它是基于jquery, 但是第三方开发的, ...

  5. Linux 命令行总结

    1.使用ln不加参数,会创建硬链接,如果要创建软连接,需要加-s 参数. # ln test1 test8 -rw-r--r-- root root Nov : test1 -rw-r--r-- ro ...

  6. tyvj1189 盖房子

    描述 永恒の灵魂最近得到了面积为n*m的一大块土地(高兴ING^_^),他想在这块土地上建造一所房子,这个房子必须是正方形的.但是,这块土地并非十全十美,上面有很多不平坦的地方(也可以叫瑕疵).这些瑕 ...

  7. NOIP2009 Hankson的趣味题

    题目描述 Description Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考一个有趣的问题.今天在 ...

  8. jaxb

    一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...

  9. PHP 5.4 已废弃 magic_quotes_gpc,PHP安全转义函数详解(addslashes 、htmlspecialchars、htmlentities、mysql_real_escape_string、strip_tags)

    1. addslashes() addslashes()对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如果D ...

  10. @好友的EditText

    类似微信聊天中的@好友功能,封装到一个EditText中,gist打不开了,直接贴代码到这里吧: /*** @好友的输入组件*/public class AtEditText extends Edit ...