Solved

detect NIC settings - c#

Posted on 2007-11-27
11
2,483 Views
Last Modified: 2009-09-17
I need the sample code that i can use to detect the network settings of the NIC.
(speed,type,duplex/half etc)

Any help is greatly appreciated
0
Comment
Question by:crystalguy2000
  • 5
  • 4
11 Comments
 
LVL 16

Expert Comment

by:gnoon
ID: 20363264
Using Win32_NetworkAdapter Class

Code sample here http://www.codeproject.com/csharp/NetworkMonitorl.asp
Code reference here http://msdn2.microsoft.com/en-us/library/Aa394216.aspx
0
 

Author Comment

by:crystalguy2000
ID: 20365605
The type or namespace name 'NetworkAdapter' could not be found (are you missing a using directive or an assembly reference?)      

have references to system.management and

using System.Net.NetworkInformation and
using System.Net

any suggestions?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20365962
It's not a class of .NET, but a class of Windows Management Instrumentation (WMI) developed by Distributed Management Task Force (DMTF) represents a network adapter of a computer running a Windows operating system (as in the microsoft link above).

Below is its properties and how to query a property from computer using ManagementObjectSearcher (snippet from links above).
class Win32_NetworkAdapter : CIM_NetworkAdapter

{

  string AdapterType;

  uint16 AdapterTypeID;

  boolean AutoSense;

  uint16 Availability;

  string Caption;

  uint32 ConfigManagerErrorCode;

  boolean ConfigManagerUserConfig;

  string CreationClassName;

  string Description;

  string DeviceID;

  boolean ErrorCleared;

  string ErrorDescription;

  string GUID;

  uint32 Index;

  datetime InstallDate;

  boolean Installed;

  uint32 InterfaceIndex;

  uint32 LastErrorCode;

  string MACAddress;

  string Manufacturer;

  uint32 MaxNumberControlled;

  uint64 MaxSpeed;

  string Name;

  string NetConnectionID;

  uint16 NetConnectionStatus;

  boolean NetEnabled;

  string NetworkAddresses[];

  string PermanentAddress;

  boolean PhysicalAdapter;

  string PNPDeviceID;

  uint16 PowerManagementCapabilities[];

  boolean PowerManagementSupported;

  string ProductName;

  string ServiceName;

  uint64 Speed;

  string Status;

  uint16 StatusInfo;

  string SystemCreationClassName;

  string SystemName;

  datetime TimeOfLastReset;

};
 

// read informations of all network adapters

ManagementObjectSearcher searcher = new 

    ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");

ManagementObjectCollection adapterObjects = searcher.Get();

foreach (ManagementObject adapterObject in adapterObjects)

{

    string adapterName    =    adapterObject["Name"];

    string sdapterType    =    adapterObject["AdapterType"];

    ...

}

Open in new window

0
 

Author Comment

by:crystalguy2000
ID: 20366544
how would i do that using c#?
0
 
LVL 16

Expert Comment

by:gnoon
ID: 20370376
See at the end of code above.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:crystalguy2000
ID: 20370839
THat did not work.

The type or namespace name 'NetworkAdapter' could not be found (are you missing a using directive or an assembly reference?)      

have references to system.management and

using System.Net.NetworkInformation and
using System.Net

any suggestions?
0
 

Author Comment

by:crystalguy2000
ID: 20370934
I tried to paste the code and compiiled.
It complains of some missing namespace..
0
 
LVL 16

Accepted Solution

by:
gnoon earned 500 total points
ID: 20371000
There's a bit syntax error in code above about type cast.
This code works on my computer.

However, some properties, e.g. speedduplex, may be not populated because manufacturer driver does not make the information available. You may try to check if they're available in the registry under

HKLM\System\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}
using System;

using System.Management;
 

class MyNetworkAdapters

{

	public static void Main() 

	{

		// read informations of all network adapters

		ManagementObjectSearcher searcher = new 

			ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter");

		ManagementObjectCollection adapterObjects = searcher.Get();

		foreach (ManagementObject adapterObject in adapterObjects)

		{

			string adapterName = (string)adapterObject["Name"];

			string adapterType = (string)adapterObject["AdapterType"];

			UInt16 connectionStatus = Convert.ToUInt16(adapterObject["NetConnectionStatus"]);

			Console.WriteLine("{0} : {1} ({2})",adapterName, adapterType, connectionStatus);

		}

		Console.Read();

	}

}

Open in new window

0
 

Author Comment

by:crystalguy2000
ID: 20371069
thanks..this one works...But as you said the speed was not populated.
But i think that should suffice for my research.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20591145
Forced accept.

Computer101
EE Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why don't I see this table in EDMX file? 2 23
Iterate a dictionnary to change values 4 53
XAML: Layout 8 25
Please explain "Multi-Tenant Services" 5 60
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now