整理UWP中网络和设备信息获取的帮助类,需要的拿走。
网络(运营商信息,网络类型)
- public static class NetworkInfo
- {
- /// <summary>
- /// 网络是否可用
- /// </summary>
- public static bool IsNetworkAvailable
- {
- get
- {
- ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
- return (profile?.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
- }
- }
- /// <summary>
- /// 获取IP地址
- /// </summary>
- /// <returns>IP地址</returns>
- public static string GetIpAddress()
- {
- Guid? networkAdapterId = NetworkInformation.GetInternetConnectionProfile()?.NetworkAdapter?.NetworkAdapterId;
- return (networkAdapterId.HasValue ? NetworkInformation.GetHostNames().FirstOrDefault(hn => hn?.IPInformation?.NetworkAdapter.NetworkAdapterId == networkAdapterId)?.CanonicalName : null);
- }
- /// <summary>
- /// 获取网络运营商信息
- /// </summary>
- /// <returns></returns>
- public static string GetNetworkName()
- {
- try
- {
- ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
- if (profile != null)
- {
- if (profile.IsWwanConnectionProfile)
- {
- var homeProviderId = profile.WwanConnectionProfileDetails.HomeProviderId;
- //4600是我手机测试出来的。
- if (homeProviderId == "" || homeProviderId == "" || homeProviderId == "")
- {
- return "中国移动";
- }
- //已验证
- else if (homeProviderId == "")
- {
- return "中国联通";
- }
- //貌似还没win10 电信手机。。待验证
- else if (homeProviderId == "")
- {
- return "中国电信";
- }
- }
- else
- {
- return "其他";
- }
- //也可以用下面的方法,已验证移动和联通
- //var name = profile.GetNetworkNames().FirstOrDefault();
- //if (name != null)
- //{
- // name = name.ToUpper();
- // if (name == "CMCC")
- // {
- // return "中国移动";
- // }
- // else if (name == "UNICOM")
- // {
- // return "中国联通";
- // }
- // else if (name == "TELECOM")
- // {
- // return "中国电信";
- // }
- //}
- //return "其他";
- }
- return "其他";
- }
- catch (Exception)
- {
- return "其他";
- }
- }
- /// <summary>
- /// 获取网络连接类型
- /// </summary>
- /// <returns></returns>
- public static string GetNetWorkType()
- {
- try
- {
- ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
- if (profile == null)
- {
- return "未知";
- }
- if (profile.IsWwanConnectionProfile)
- {
- WwanDataClass connectionClass = profile.WwanConnectionProfileDetails.GetCurrentDataClass();
- switch (connectionClass)
- {
- //2G-equivalent
- case WwanDataClass.Edge:
- case WwanDataClass.Gprs:
- return "2G";
- //3G-equivalent
- case WwanDataClass.Cdma1xEvdo:
- case WwanDataClass.Cdma1xEvdoRevA:
- case WwanDataClass.Cdma1xEvdoRevB:
- case WwanDataClass.Cdma1xEvdv:
- case WwanDataClass.Cdma1xRtt:
- case WwanDataClass.Cdma3xRtt:
- case WwanDataClass.CdmaUmb:
- case WwanDataClass.Umts:
- case WwanDataClass.Hsdpa:
- case WwanDataClass.Hsupa:
- return "3G";
- //4G-equivalent
- case WwanDataClass.LteAdvanced:
- return "4G";
- //not connected
- case WwanDataClass.None:
- return "未连接";
- //unknown
- case WwanDataClass.Custom:
- default:
- return "未知";
- }
- }
- else if (profile.IsWlanConnectionProfile)
- {
- return "WIFI";
- }
- return "未知";
- }
- catch (Exception)
- {
- return "未知"; //as default
- }
- }
- }
设备信息(分辨率,设备类型(PC,平板,手机,Xbox))
- /// <summary>
- /// 设备信息
- /// </summary>
- public static class DeviceInfo
- {
- /// <summary>
- /// 设备ID
- /// </summary>
- public static readonly string DeviceId;
- /// <summary>
- /// 用户代理
- /// </summary>
- public static readonly string UserAgent;
- /// <summary>
- /// 操作系统版本
- /// </summary>
- public static readonly string OsVersion;
- /// <summary>
- /// 设备分辨率
- /// </summary>
- public static readonly Size DeviceResolution;
- /// <summary>
- /// 设备时区名字
- /// </summary>
- public static readonly string Timezone;
- /// <summary>
- /// 设备语言
- /// </summary>
- public static readonly string Language;
- /// <summary>
- /// 设备类型
- /// </summary>
- public static readonly string DeviceType;
- static DeviceInfo()
- {
- DeviceId = GetDeviceId();
- UserAgent = GetUserAgent();
- OsVersion = GetOsVersion();
- DeviceResolution = GetDeviceResolution();
- Timezone = GetTimezone();
- Language = GetLanguage();
- DeviceType = GetDeviceType();
- }
- private static string GetDeviceType()
- {
- var deviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily;
- if (deviceFamily == "Windows.Desktop")
- {
- if (UIViewSettings.GetForCurrentView().UserInteractionMode == UserInteractionMode.Mouse)
- {
- return "WINDESKTOP";
- }
- else
- {
- return "WINPAD";
- }
- }
- else if (deviceFamily == "Windows.Mobile")
- {
- return "WINPHONE";
- }
- else if (deviceFamily == "Windows.Xbox")
- {
- return "XBOX";
- }
- else if (deviceFamily == "Windows.IoT")
- {
- return "IOT";
- }
- else
- {
- return deviceFamily.ToUpper();
- }
- }
- /// <summary>
- /// 获取设备语言
- /// </summary>
- /// <returns>设备语言</returns>
- private static string GetLanguage()
- {
- var Languages = Windows.System.UserProfile.GlobalizationPreferences.Languages;
- if (Languages.Count > )
- {
- return Languages[];
- }
- return Windows.Globalization.Language.CurrentInputMethodLanguageTag;
- }
- /// <summary>
- /// 获取设备时区名字
- /// </summary>
- /// <returns>设备时区名字</returns>
- private static string GetTimezone()
- {
- return TimeZoneInfo.Local.DisplayName;
- }
- /// <summary>
- /// 获取设备分辨率
- /// </summary>
- /// <returns>设备分辨率</returns>
- private static Size GetDeviceResolution()
- {
- Size resolution = Size.Empty;
- var rawPixelsPerViewPixel = DisplayInformation.GetForCurrentView().RawPixelsPerViewPixel;
- foreach (var item in PointerDevice.GetPointerDevices())
- {
- resolution.Width = item.ScreenRect.Width * rawPixelsPerViewPixel;
- resolution.Height = item.ScreenRect.Height * rawPixelsPerViewPixel;
- break;
- }
- return resolution;
- }
- /// <summary>
- /// 获取设备ID
- /// </summary>
- /// <returns>设备ID</returns>
- private static string GetDeviceId()
- {
- HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
- return CryptographyHelper.Md5Encrypt(token.Id);
- }
- /// <summary>
- /// 获取用户代理
- /// </summary>
- /// <returns>用户代理</returns>
- private static string GetUserAgent()
- {
- var Info = new EasClientDeviceInformation();
- return $"{Info.SystemManufacturer} {Info.SystemProductName}";
- }
- /// <summary>
- /// 获取操作系统版本
- /// </summary>
- /// <returns>操作系统版本</returns>
- private static string GetOsVersion()
- {
- ulong version = Convert.ToUInt64(AnalyticsInfo.VersionInfo.DeviceFamilyVersion);
- return $"{version >> 48 & 0xFFFF}.{version >> 32 & 0xFFFF}.{version >> 16 & 0xFFFF}.{version & 0xFFFF}";
- }
- }
- /// <summary>
- /// 加密帮助类
- /// </summary>
- public static class CryptographyHelper
- {
- public static string DesEncrypt(string key, string plaintext)
- {
- SymmetricKeyAlgorithmProvider des = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.DesEcbPkcs7);
- IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
- CryptographicKey symmetricKey = des.CreateSymmetricKey(keyMaterial);
- IBuffer plainBuffer = CryptographicBuffer.ConvertStringToBinary(plaintext, BinaryStringEncoding.Utf8);
- IBuffer cipherBuffer = CryptographicEngine.Encrypt(symmetricKey, plainBuffer, null);
- return CryptographicBuffer.EncodeToHexString(cipherBuffer);
- }
- public static string TripleDesDecrypt(string key, string ciphertext)
- {
- SymmetricKeyAlgorithmProvider tripleDes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcb);
- IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
- CryptographicKey symmetricKey = tripleDes.CreateSymmetricKey(keyMaterial);
- IBuffer cipherBuffer = CryptographicBuffer.DecodeFromHexString(ciphertext);
- IBuffer plainBuffer = CryptographicEngine.Decrypt(symmetricKey, cipherBuffer, null);
- return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, plainBuffer);
- }
- public static string Md5Encrypt(string value)
- {
- IBuffer data = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
- return Md5Encrypt(data);
- }
- public static string Md5Encrypt(IBuffer data)
- {
- HashAlgorithmProvider md5 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
- IBuffer hashedData = md5.HashData(data);
- return CryptographicBuffer.EncodeToHexString(hashedData);
- }
- public static string EncodeToBase64String(string value)
- {
- IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
- return CryptographicBuffer.EncodeToBase64String(buffer);
- }
- public static string DecodeFromBase64String(string value)
- {
- IBuffer buffer = CryptographicBuffer.DecodeFromBase64String(value);
- return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, buffer);
- }
- }
整理UWP中网络和设备信息获取的帮助类,需要的拿走。的更多相关文章
- Centos7中网络及设备相关配置
centos7中,不再赞成使用ifconfig工具,取而代之的是nmcli工具,服务管理也是以systemctl工具取代了service,这些之前版本的工具虽然在centos7中还可以继续使用,只是出 ...
- 获取设备信息——获取客户端ip地址和mac地址
1.获取本地IP(有可能是 内网IP,192.168.xxx.xxx) /** * 获取本地IP * * @return */ public static String getLocalIpAddre ...
- 史上最全的iOS各种设备信息获取总结
来源:si1ence 链接:http://www.jianshu.com/p/b23016bb97af 为了统计用户信息.下发广告,服务器端往往需要手机用户设备及app的各种信息,下面讲述一下各种信息 ...
- iOS: iOS各种设备信息获取
Author:si1ence Link:http://www.jianshu.com/p/b23016bb97af 为了统计用户信息.下发广告,服务器端往往需要手机用户设备及app的各种信息,下面讲述 ...
- iOS 设备信息获取
參考:http://blog.csdn.net/decajes/article/details/41807977參考:http://zengrong.net/post/2152.htm1. 获取设备的 ...
- Python 网络爬虫与信息获取(一)—— requests 库的网络爬虫
1. 安装与测试 进入 cmd(以管理员权限),使用 pip 工具,pip install requests 进行安装: 基本用法: >> import requests >> ...
- 亚马逊商品页面的简单爬取 --Pyhon网络爬虫与信息获取
1.亚马逊商品页面链接地址(本次要爬取的页面url) https://www.amazon.cn/dp/B07BSLQ65P/ 2.代码部分 import requestsurl = "ht ...
- 京东某商品页面的简单爬取 --Pyhon网络爬虫与信息获取
1.京东商品页面链接地址(本次要爬取的页面url) https://item.jd.hk/1953999200.html 2.代码部分 import requestsurl = "https ...
- Python 网络爬虫与信息获取(二)—— 页面内容提取
1. 获取超链接 python获取指定网页上所有超链接的方法 links = re.findall(b'"((http|ftp)s?://.*?)"', html) links = ...
随机推荐
- bzoj 1014 splay维护hash值
被后缀三人组虐了一下午,写道水题愉悦身心. 题很裸,求lcq时二分下答案就行了,写的不优美会被卡时. (写题时精神恍惚,不知不觉写了快两百行...竟然调都没调就A了...我还是继续看后缀自动机吧... ...
- 做参数可以读取参数 保存参数 用xml文件的方式
做参数可以读取参数 保存参数 用xml文件的方式 好处:供不同用户保存适合自己使用的参数
- elasticsearch snapshot
一.Repositories 在elasticsearch.yml文件中增加path.repo路径配置: $ vim /etc/elasticsearch/elasticsearch.yml path ...
- jquery-easyui 树的使用笔记
通常还是使用jquery-ui, 它是完全免费的, jquery-easyui可以使用 freeware edition. 但easyui还不是完全免费的: 它是基于jquery, 但是第三方开发的, ...
- Linux 命令行总结
1.使用ln不加参数,会创建硬链接,如果要创建软连接,需要加-s 参数. # ln test1 test8 -rw-r--r-- root root Nov : test1 -rw-r--r-- ro ...
- tyvj1189 盖房子
描述 永恒の灵魂最近得到了面积为n*m的一大块土地(高兴ING^_^),他想在这块土地上建造一所房子,这个房子必须是正方形的.但是,这块土地并非十全十美,上面有很多不平坦的地方(也可以叫瑕疵).这些瑕 ...
- NOIP2009 Hankson的趣味题
题目描述 Description Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考一个有趣的问题.今天在 ...
- jaxb
一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...
- PHP 5.4 已废弃 magic_quotes_gpc,PHP安全转义函数详解(addslashes 、htmlspecialchars、htmlentities、mysql_real_escape_string、strip_tags)
1. addslashes() addslashes()对SQL语句中的特殊字符进行转义操作,包括(‘), (“), (), (NUL)四个字符,此函数在DBMS没有自己的转义函数时候使用,但是如果D ...
- @好友的EditText
类似微信聊天中的@好友功能,封装到一个EditText中,gist打不开了,直接贴代码到这里吧: /*** @好友的输入组件*/public class AtEditText extends Edit ...