"Win 10 app development" Gets the IP address of this machine

Source: Internet
Author: User

According to the usual, but also a friend's advice, the old week before bragging, tell a small story.

A friend asked me, Old week, do you still text, how much do you use the telephone fee per month? Alas, to tell the truth, now really texting is not much, the package sent 130 text messages, each month can not send a. As for the telephone fee, basically is to pay monthly rent, the call fee may average less than 1 yuan, more likely on a few dollars. Old weeks of a single talk time is generally not long, up to half an hour of the call is also accidental, most of the cases say a few words just, don't talk nonsense, long story short, said finished hanging machine.

Some people will ask, if you want to talk for a while, then open a chat tool, chat voice. Anyway, with a fiber-optic package, say a few more nonsense also do not spend money.

The province of the province's money, the old week has always been rich and not casually spent, the use of the money to buy some can add value to the collection, or make some risk not too big investment is also good. Many times, the old week would rather buy a handicraft on the desk, will not buy movie tickets. Think about buying a movie ticket, look at two hours will be finished, nothing left, and again, in recent years, too much rotten film, now the director writers, artistic accomplishment is too low, to take a number of challenges to the bottom of the film, see this movie is not worth, these ghost pieces at most 1 cents. I might as well buy something with a level of taste at home, I can also see every day.

===============================================================

Well, the story is over, let's talk about it today. How to get the IP address of the native in the UWP app. I do not remember having written a similar article before, I do not know if anyone has written it, if you understand, you do not have to look at the following, you will be the old week pull the crap on the line.

The old weeks of blogging has always been very casual, absolutely not serious, not the old weeks of knowledge is not serious, but the old week temperament so, never like too much restraint, therefore, rather than blog, rather than the essay, it is free to write, as long as not hurt the good.

In the Windows.Networking.Connectivity namespace, there is a class called NetworkInformation, it has a static public method called Gethostnames, call it can get the machine name of the machine.

Some friends will say, this is so damn simple, don't be happy too early, you use it to know. The method call is simple, however it returns n hostname instance, this can be a bit xx, multiple hostname, which do I choose?

First look, the hostname class has a Type property, it is a Hostnametype enumeration value, if it is a computer name (such as "MyPC"), the type is domainname, if it is the address on the Bluetooth network card, then Bluetooth, if the IP address, There are two values of--ipv4 and Ipv6.

In most cases, the IP address will be used, so you will think that when the Networkinformation.gethostnames () method is called to get the hostname list, you can use its Type property to filter out the hostname instance that represents the IP address. The V4 IP address, such as 192.168.0.20, is now used much more.

OK, the first problem is solved, but the new problem comes, although I have filtered out the hostname that represents the IP, but I want to use the IP address of the Wi-Fi connection, what should I do?

It's okay, let's analyze it. When you confirm that a hostname object is an IP address representation, you can access its Ipinformation property, and you will get a ipinformation instance.

Later, you will find that the Ipinfomation class has a NetworkAdapter property, accesses it, and obtains a NetworkAdapter instance. This NetworkAdapter instance represents the information of the network adapter that this hostname represents, and you may want to understand that it is the network card that is connected (including the wireless card, of course).

Then you find that the NetworkAdapter class has a property called Ianainterfacetype, which is an int type.

Now, you are only a step away from the truth. This integer value is not to be underestimated, it can be used to identify the network interface type. These values are available on MSDN, and for everyone's sake, I've copied some of them down, please see:

6--Ethernet;

23--PPP Network Interface (special terminology, you can search online, to understand the line, do not rote);

24--software loopback interface;

37--ATM network interface;

71--Wireless network Interface (this is the focus);

Other interfaces.

To determine whether it is a wireless network interface, you can see if it is 71.

At this point, I believe that everyone in the hearts of the doubts have dissipated almost, do not know the old weeks to explain, you know?

No, it's up to the code.

            //get a list of local host names            varhosts =Networkinformation.gethostnames (); //Filter Wireless or Ethernet            varHost = hosts. FirstOrDefault (h =             {                 BOOLIsipaddr = (H.type = = Windows.Networking.HostNameType.Ipv4) | | (H.type = =Windows.Networking.HostNameType.Ipv6); //if it is not the name represented by the IP address, ignore the                 if(Isipaddr = =false)                 {                     return false; } ipinformation Ipinfo=h.ipinformation; //71 means wireless, 6 means Ethernet                 if(Ipinfo.NetworkAdapter.IanaInterfaceType = = in|| Ipinfo.NetworkAdapter.IanaInterfaceType = =6)                 {                     return true; }                 return false;            }); if(Host! =NULL) {TB. Text= host. DisplayName;//Display IP}

The results of the run are as shown.

OK, this time the content is here, another day, and we talk about the Socket related topics.

Sample source Code

"Win 10 app development" Gets the IP address of this machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.