Click here to Skip to main content
15,795,398 members
Articles / Programming Languages / C#

SNMP library

Rate me:
Please Sign up or sign in to vote.
4.56/5 (62 votes)
20 Apr 2006CPOL5 min read 1.4M   57.6K   144   254
Make SNMP request to get or set value on your computer, CISCO server, server in general, appliance,...


Dear friends, ladies and gentleman welcome to the fabulous world of SNMP... With SNMP you can check many status of a server, switch, computer or whatever, you can modify values or parameters of that status. So a lot of thing very interesting...

Get value or set value by an SNMP request. How easy it should be, how easy I though it would be. With linux you can use the command snmpget or snmpset and you make the query and get your answer in 5 seconds.

I wanted the same with windows or more precisely I wanted to do it from a C# code. The idea was to create a very simple interface (dll) to use it in many applications. I didn't find any library easy to use, I didn't find exactly what I want.

So here it is... I create to project : one for the library-dll and another to test this dll and to show you how it's work.

First look !

Console.WriteLine("test simple get");

//Here is the point
SNMPObject s = new SNMPObject("");
//With this OID you can get the name 
//of the computer/server you request

string myValue = s.getSimpleValue(new SNMPAgent(""));
//Look on my computer

Console.WriteLine("My value is : " + myValue);

The result is :
Test simple get
My value is MyComputer-One

In two lines you have what you want...


What do we need... 2 things + 1 optional thing

The SNMPAgent : It's the server/computer requested. This server is defined by 2 variables : the IP address and the community string which is "public" by default. The community string is like a password.

The SNMPObject : It's the request itself. This object is defined by an OID. An OID is a series of number separated by a dot which describe the "place" where lives a kind of value (ex : name of computer =

The Mib(Optional) : This object make the link between the OID and what it is. If you already know the OID you want, you don't need the Mib. But if you want to know for example the load of the CPU and do not know the OID, you need the Mib to know it. It's similar as a DNS with IP address.

Test and explanation:


//LOAD all the MIB found into the directory c:\windows\System32\****************
    Mib myMib = new Mib();
  Console.WriteLine("**************Loading MIB's files**************");

 //**************DISPLAY THE loading MIB************************

 //*************INIT SNMP AGENT*********************
  SNMPAgent myAgent = new SNMPAgent("","public","public");

  //if you do that please active the snmp capabilities
  //into your compture : "Add/remove windows component"
  //Becarefull => a firewall that stop traffic from localhost to localhost
  //will prevent this test to work. 
  //- "Management and monitoring tools"
  //************INIT SNMP OBJECT*********************
  SNMPObject myRequest = new SNMPObject("",myMib);
  Console.WriteLine("***************Make the request***************");
  Console.WriteLine("I am looking for the value : " + 
  Console.WriteLine("My value is : " + myRequest.getSimpleValue(myAgent));
  Console.WriteLine("The type of my value is : " + myRequest.getType());
  Console.WriteLine("The description of my value is : " + 

 //**********SOME OTHER TEST**********************


MIB loading

The first part of the code loads different mib files, usually on your computer in c:\windows\System32. As you should see all the files are read without any problem.
If you want you can walk into that mib to see all the value-OID-description found in the mib's files (by making myMib.walk()).
My experience shows me that sometimes a mib file cannot be loaded because the parser is not enough good to understand it. But most of the times there will be no problem.

When you create the object "Mib", it loads the files " SNMPv2-SMI.mib" which is hardcoded and is the base of all mib.
Remember you don't need to create an object Mib to make SNMP request, it's just a link between the OID, his name and his description.

Get value

This is the simplest thing you can do. First you need to defined a SNMP agent, then an SNMPObject and finally you can get the value and the type of this value.

If you give in argument, when you create the SNMPObject, a Mib object, you can also get the description and the full name of the OID.
This is the point I never found in any free source code in c#: Mix an MIB parser and an SNMP requester...

You can also get multiple value in one shot. See inside the procedure testMultiGet().


If you can to launch an SNMP request on all OID starting by "" just launch the command : myAgent.walk(new SNMPObject(""))


The set command is the last thing you can do. Be sure that you have the right to make the command on the given OID.
In this example the set command won't work because you can not change the name of your computer by an SNMP request.


Here is what you should get after

**************Loading MIB's files**************
  Load C:\WINDOWS\System32\accserv.mib
  Load C:\WINDOWS\System32\authserv.mib
  Load C:\WINDOWS\System32\dhcp.mib
  Load C:\WINDOWS\System32\ftp.mib
  Load C:\WINDOWS\System32\hostmib.mib
  Load C:\WINDOWS\System32\http.mib
  Load C:\WINDOWS\System32\inetsrv.mib
  Load C:\WINDOWS\System32\ipforwd.mib
  Load C:\WINDOWS\System32\lmmib2.mib
  Load C:\WINDOWS\System32\mcastmib.mib
  Load C:\WINDOWS\System32\mib_ii.mib
  Load C:\WINDOWS\System32\mipx.mib
  Load C:\WINDOWS\System32\mripsap.mib
  Load C:\WINDOWS\System32\msft.mib
  Load C:\WINDOWS\System32\msipbtp.mib
  Load C:\WINDOWS\System32\msiprip2.mib
  Load C:\WINDOWS\System32\nipx.mib
  Load C:\WINDOWS\System32\smi.mib
  Load C:\WINDOWS\System32\wfospf.mib
  Load C:\WINDOWS\System32\wins.mib

***************Make the request***************
  I am looking for the value : mgmt.mib-2.system.sysName.0
  My value is : MYCOMPUTER-ONE
  The type of my value is : OctetString
  The description of my value is : "An administratively-assigned 
  name for this managed node. By convention, this is the node's
  fully-qualified domain name."

  test walk

  Rentre dans le Walk
  []:,MS TCP Loopback interface ,OctetString
  []:,3Com EtherLink XL 10/100 PCI 
          TX NIC (3C905B-TX) - Packet Scheduler Miniport, OctetString
  []:, P♦r☺?,OctetString

  test multi get
  oid :
  type : OctetString
  oid :
  value : 298230716
  type : Counter32

  test Set
  Read the value before the modification
  oid :
  type : OctetString

  Set the value
  Error: SNMP: Variable does not exist; status: NoSuchName; index: 0
  *** SNMP protocol error while processing <op>:
  SNMP error status: NoSuchName
  SNMP error index: 0
  Error: SNMP: Variable does not exist; status: NoSuchName; index: 0

  Read the value after the modification
  oid :
  type : OctetString

Points of Interest

Firstly I needed the possibilities to make some SNMP request. After using it, I saw the billion of possibilities SNMP can give me. So now I use SNMP every days in a pleiad of projects.

I hope you'll get fun with SNMP as I do.


I based this dll from 4 other dll dispatched in 2 parts :

SnmpComp and tableReader with the namespace : Org.Snmp.Snmp_pp. This dll allow me to make any snmp request I want it. It's quite difficult to understand at first, it's difficult to compile but when you are in... well, it should be ok. Here is the web site where you can find it : Please note that I modified the original dll to adapt it to my need, to the current project.

mib and tools with the namespace : RFC1157 . This dll allow me to read a MIB files, to parse it and finaly to make the link between an OID, its type, its description and its fullname. It was developped by Malcolm Crowe and you can find the code : Take care that some of mib files cannot be read by his parser...

This project was born with these two priceless sources...

Feel free to send feed back, I'll correct all the error you may see...

- 8 February 2006 : release version 1.0 of SNMPDLL
- 20 March 2006 : add the source code of snmp_ppnet (for the last version see This code source is only for people who want to see the code of SNMPComp.dll used by SNMPDLL.dll


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

Written By
Web Developer
Belgium Belgium

You'll be surprise about this incredible tool !

Comments and Discussions

QuestionProject Pin
Member 1603556123-Jun-23 7:21
Member 1603556123-Jun-23 7:21 
QuestionI ran the exe and it throws timeout exception. Am I missing something Pin
Samir Joshi3-Feb-21 20:13
Samir Joshi3-Feb-21 20:13 
QuestionBad Image Format Exception was Unhandled Pin
Member 829385831-Oct-16 2:29
Member 829385831-Oct-16 2:29 
QuestionConfiguration Type of SnmpComp.dll Pin
Vimesh Shah2-Mar-15 17:59
Vimesh Shah2-Mar-15 17:59 
QuestionBuild of SnmpComp.dll Pin
Vimesh Shah26-Feb-15 2:26
Vimesh Shah26-Feb-15 2:26 
Questionnull referenceexception was unhandled Pin
Member 1143821816-Feb-15 0:09
Member 1143821816-Feb-15 0:09 
QuestionSNMP variable does not exist, NoSuchName; index: 1 Pin
carlos ariel alt2-Feb-15 9:53
carlos ariel alt2-Feb-15 9:53 
QuestionLoadmib throws exception Pin
sagar_25324-Jun-14 2:00
sagar_25324-Jun-14 2:00 
QuestionStackOverFlowexception Pin
sagar_25323-Jun-14 21:18
sagar_25323-Jun-14 21:18 
AnswerRe: StackOverFlowexception Pin
Member 1086297713-Jul-14 21:18
Member 1086297713-Jul-14 21:18 
Questionerror in conversion OctecString Pin
Member 1006039611-Jun-14 14:26
Member 1006039611-Jun-14 14:26 
QuestionError on Visual Studio 2012 Pin
Filipe D. Silva23-Oct-13 7:18
professionalFilipe D. Silva23-Oct-13 7:18 
QuestionHelp me restore Sysname Value Pin
Member 1020754414-Aug-13 2:21
Member 1020754414-Aug-13 2:21 
Questionhowever the set dosen't work? Pin
Member 1006039620-May-13 13:12
Member 1006039620-May-13 13:12 
QuestionGet ink and ERROR Pin
Itamar Tziger19-Apr-13 0:29
Itamar Tziger19-Apr-13 0:29 
QuestionWindows 7 Pin
Member 997609213-Apr-13 13:40
Member 997609213-Apr-13 13:40 
Questionexeption Pin
soheaila16-Mar-13 9:04
soheaila16-Mar-13 9:04 
AnswerRe: exeption Pin
Lex Li18-Mar-13 18:05
professionalLex Li18-Mar-13 18:05 
Questionsnmp c sharp Pin
Member 98700584-Mar-13 4:05
Member 98700584-Mar-13 4:05 
AnswerRe: snmp c sharp Pin
Lex Li18-Mar-13 18:05
professionalLex Li18-Mar-13 18:05 
GeneralRe: snmp c sharp Pin
Member 987005819-Mar-13 1:27
Member 987005819-Mar-13 1:27 
Question#SNMP Suite 7.6 is released Pin
Lex Li22-Dec-12 19:23
professionalLex Li22-Dec-12 19:23 
AnswerRe: #SNMP Suite 7.6 is released Pin
Lex Li31-Jan-13 4:54
professionalLex Li31-Jan-13 4:54 
QuestionSNMP Trap messages Pin
shagil4-Oct-12 0:00
shagil4-Oct-12 0:00 
AnswerRe: SNMP Trap messages Pin
Lex Li11-Oct-12 3:54
professionalLex Li11-Oct-12 3:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.