which network am i on

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
i have a test network that's exactly like my prod network (except for the
FQDN) and all i have to do is move my network cable from one network to the
other.

sometimes i forget which network i'm on while working on an application. do
you know of some different ways in csharp i can check to determine which
network i'm on and setup some sort of indicator on the page?

thanks,
rodchar
 
hey all,
i have a test network that's exactly like my prod network (except for the
FQDN) and all i have to do is move my network cable from one network to
the
other.

sometimes i forget which network i'm on while working on an
application. do
you know of some different ways in csharp i can check to determine which
network i'm on and setup some sort of indicator on the page?

There's no standardized way to do that. But if you know something
specific about each network that produces a unique difference -- for
example, each uses a different subnet (so you can check the IP address
assigned to your adapter), or there's a server on one or the other that
you can ask -- then you could include code to check for that.

Without knowing more about your network, it's not possible to say what
exactly would work for you.

Pete
 
how can i check an ip address in csharp?

Peter Duniho said:
There's no standardized way to do that. But if you know something
specific about each network that produces a unique difference -- for
example, each uses a different subnet (so you can check the IP address
assigned to your adapter), or there's a server on one or the other that
you can ask -- then you could include code to check for that.

Without knowing more about your network, it's not possible to say what
exactly would work for you.

Pete
 
how can i check an ip address in csharp?

There are a variety of ways to do it, depending on what information you
need exactly. But if all you want is a list of IP addresses assigned to
the local host, you can call Dns.GetHostAddresses("").

Pete
 
how about the Default Gateway IP Address, can i get to that because i notice
that was different?
 
how about the Default Gateway IP Address, can i get to that because i
notice
that was different?

Unless your local host is the default gateway, then yes...it would be
different, since the default gateway is an entirely different piece of
network hardware.

As far as how to get that information, I don't recall off the top of my
head whether a HostEntry (which you can get from Dns.GetHostEntries())
provides that or not. If not, you may need to find the networking API in
WMI and get it from there.

I recommend doing a little browsing in the MSDN docs. At this point,
since I don't know from memory the answer to your question, that'd be what
I'd have to do in order to provide an answer anyway.

Pete
 
rodchar said:
thanks for the help and pointing in the right direction,
rod.




Following small sample (using WMI/System.Management), dumps some of the
"connected" adapter's configuration info ...

using System;
using System.Management;

class Sample_NeworkSelectQuery
{
enum NetConnectionStatus
{
Connecting = 1,
Connected,
Disconnecting,
Hardware_not_present,
Hardware_disabled,
Hardware_malfunction
}
public static void Main(string[] args)
{
// search for the physical adapters with connection status = 2
(connected)
string q = string.Format("Select DeviceId, NetConnectionStatus ,
NetConnectionID from Win32_NetworkAdapter where PhysicalAdapter='{0}' and
NetConnectionStatus={1}",
true,
(int)NetConnectionStatus.Connected);

SelectQuery selectQuery = new SelectQuery(q);
using(ManagementObjectSearcher adapterSearcher = new
ManagementObjectSearcher(selectQuery))
{
foreach (ManagementObject adapter in adapterSearcher.Get())
{
Console.WriteLine("{0},
{1}",adapter["NetConnectionStatus"].ToString(),
adapter["NetConnectionID"].ToString());
RelatedObjectQuery adapterRelatedQuery =
new RelatedObjectQuery ("associators of
{Win32_NetworkAdapter.DeviceId='" + adapter["DeviceId"]+ "'}");
adapterRelatedQuery.RelatedClass =
"Win32_NetworkAdapterConfiguration";
using(ManagementObjectSearcher adapterConfigSearcher = new
ManagementObjectSearcher(adapterRelatedQuery))
{
foreach (ManagementObject adapterConfig in
adapterConfigSearcher.Get())
{
foreach(string ip in adapterConfig["IPAddress"] as
Array)
Console.WriteLine("\tIpAddress: {0}", ip);
}
}
}
}
}
}


Willy.
 
thanks for the info Willy,
rod.

Willy Denoyette said:
rodchar said:
thanks for the help and pointing in the right direction,
rod.




Following small sample (using WMI/System.Management), dumps some of the
"connected" adapter's configuration info ...

using System;
using System.Management;

class Sample_NeworkSelectQuery
{
enum NetConnectionStatus
{
Connecting = 1,
Connected,
Disconnecting,
Hardware_not_present,
Hardware_disabled,
Hardware_malfunction
}
public static void Main(string[] args)
{
// search for the physical adapters with connection status = 2
(connected)
string q = string.Format("Select DeviceId, NetConnectionStatus ,
NetConnectionID from Win32_NetworkAdapter where PhysicalAdapter='{0}' and
NetConnectionStatus={1}",
true,
(int)NetConnectionStatus.Connected);

SelectQuery selectQuery = new SelectQuery(q);
using(ManagementObjectSearcher adapterSearcher = new
ManagementObjectSearcher(selectQuery))
{
foreach (ManagementObject adapter in adapterSearcher.Get())
{
Console.WriteLine("{0},
{1}",adapter["NetConnectionStatus"].ToString(),
adapter["NetConnectionID"].ToString());
RelatedObjectQuery adapterRelatedQuery =
new RelatedObjectQuery ("associators of
{Win32_NetworkAdapter.DeviceId='" + adapter["DeviceId"]+ "'}");
adapterRelatedQuery.RelatedClass =
"Win32_NetworkAdapterConfiguration";
using(ManagementObjectSearcher adapterConfigSearcher = new
ManagementObjectSearcher(adapterRelatedQuery))
{
foreach (ManagementObject adapterConfig in
adapterConfigSearcher.Get())
{
foreach(string ip in adapterConfig["IPAddress"] as
Array)
Console.WriteLine("\tIpAddress: {0}", ip);
}
}
}
}
}
}


Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top