Matt West Matt West - 1 month ago 18
ASP.NET (C#) Question

How to splice IP address into network address in C# / asp.net?

I'm working on a service desk application that returns a variety of information on the local network of a PC.

What I want to do is take the ip address of the PC - say 10.20.30.1 / 24, and get just the network octets. 10.20.30 in this case. However, even if the network was something like 100.15.1 it would still need to work.

I'd then want to add on different device numbers. For instance, if all the switches in a network fall in the 230 - 235 range by policy, I'd want to be able to cycle through and ping those numbers looking for active switches.

Right now I'm only working with /24 networks.

So far I'm pulling the IP address of the PCs fine, but I'm a little lost on how to proceed.. Should I change the IP into a string and use some kind of regex method to strip the first three octets? That feels a little clunky.

I don't mind reading up on new methods, etc, I'm just looking to get pointed in the right direction, so to speak.

Answer Source

Just

var s = "10.20.30.1 / 24";
s = s.Substring(0, s.LastIndexOf(".")); //"10.20.30"