Click here to Skip to main content
15,898,010 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear guys, I need help.

I have C# windows form application, and I want to open form depend on PC IP address.
The idea is to check PC IP address if it is between list of IP Address.
If it is true open.

What I have tried:

Actually I don't have idea to do that. Therefore, I need help. I appreciate that.
Posted
Updated 18-Jun-21 19:25pm

1 solution

That's going to depend on what you think of as the "PC IP Address": it has two and both of those are a problem for you.

The first is the LAN / WLAN address: the address that computers on your side of the router use to talk to each other / the router. Normally, that's of the form 192.168.xxx.yyy and that's a problem because unless specific actions are taken (which differ depending on the local DHCP controller - normally the router) to assign a static IP address to a device then it gets a new IP address every time it is restarted and that means what address it gets depends on the order in which machines are turned on, and how many of them active there are.
It's also trivial to "spoof" an IP address using a VM, so starting your app in a VM means it doesn't "know" if an IP address is "real" or not.

But, getting that IP address is pretty simple:
C#
IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
    if (ip.AddressFamily == AddressFamily.InterNetwork)
    {
        ... this is the ip address you want ...
    }
}
Do also remember that a machine can have multiple IP LAN / WLAN addresses if it has multiple adaptors: one for CAT 5 wired and one for WiFi for example.

The other IP address is the internet IP address and that's a major problem. Not only is it only obtainable by querying an internet site (Google will find you many ways to do that using different sites which may or may not be "up" any more: get internet IP address c# - Google Search[^]) but ... it's not unique. It's actually the IP address of the router, and it's shared between all the devices connected to that router, as well as being allocated by your ISP when your router boots up. And that too is usually dynamic unless special action is taken (which normally involves paying extra) to use a static IP address.

So ... I think you need to rethink this idea - it's unlikely that the IP address will give you anything useful in the real world, and certainly should never be used as a security measure if that's what you were planning!
 
Share this answer
 
Comments
Karam Ibrahim 21-Jun-21 8:31am    
Dear OriginalGriff, I put the IP address on computer, and I know how to show the IP address. It is easy,
here I want to get that IP and compare with list of IP if the IP between IP1 to IPn so do it, and so.

Regards,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900