Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

which network am i on

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rodchar - 13 Mar 2008 01:28 GMT
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
Peter Duniho - 13 Mar 2008 02:21 GMT
> hey all,
> i have a test network that's exactly like my prod network (except for the
[quoted text clipped - 6 lines]
> 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
rodchar - 13 Mar 2008 13:57 GMT
how can i check an ip address in csharp?

> > hey all,
> > i have a test network that's exactly like my prod network (except for the
[quoted text clipped - 17 lines]
>
> Pete
Peter Duniho - 13 Mar 2008 18:33 GMT
> 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
rodchar - 13 Mar 2008 20:05 GMT
how about the Default Gateway IP Address, can i get to that because i notice
that was different?

> > how can i check an ip address in csharp?
>
[quoted text clipped - 3 lines]
>
> Pete
Peter Duniho - 13 Mar 2008 20:17 GMT
> 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 - 13 Mar 2008 21:03 GMT
thanks for the help and pointing in the right direction,
rod.

> > how about the Default Gateway IP Address, can i get to that because i  
> > notice
[quoted text clipped - 14 lines]
>
> Pete
Willy Denoyette [MVP] - 13 Mar 2008 22:13 GMT
> thanks for the help and pointing in the right direction,
> rod.
[quoted text clipped - 18 lines]
>>
>> Pete

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.
rodchar - 14 Mar 2008 05:02 GMT
thanks for the info Willy,
rod.

> > thanks for the help and pointing in the right direction,
> > rod.
[quoted text clipped - 77 lines]
>
> Willy.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.