Skip to content

How to use tik4net library

Daniel Frantik edited this page Oct 3, 2019 · 21 revisions

The main entry point is mikrotik router connection object (ITikConnection). Instance of connection object should be instancied via factory (ConnectionFactory).

using tik4net;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api_v2, HOST, USER, PASS);

When using tik4Net library, you should consider which API you would like to use:

See APIs comparison examples wiki page.

Low-level API

  • Low-level API: first option is to use low-level API like other mikrotik libraries do. It allows you to send API commands to mikrotik router and read response to these command.
// expectes opened connection - see example above
string[] command = new string[] { "/interface/print" };
IEnumerable<ITikSentence> result = Connection.CallCommandSync(command);
// returns list of respose sentences

ADO.NET like API

  • ADO.NET like API: second option is to use ADO.NET like API. This API tries to be very simmilar to database API used in .NET world. It allows you to create ITikCommand commands, use parameters and other features like ADO.NET does.
// expectes opened connection - see example above
ITikCommand cmd = connection.CreateCommand("/system/identity/print");
string identity = cmd.ExecuteScalar();
Console.WriteLine("Identity: " + identity);

High-level API with O/R mapper

  • High-level API: third option is to use High-level API with strongly typed entities. This API uses internal O/R mapper to map mikrotik response sentences to prepared (or your own) objects.
using tik4net;
using tik4net.Objects;
// expectes opened connection - see example above
var interfaces = connection.LoadAll<Interface>();

How to encode non-ASCII characters

  • It is easy to set connection encoding - just use ITikConnection.Encoding property.
using tik4net;
// ...
connection = ConnectionFactory.OpenConnection(TikConnectionType.Api, HOST, USER, PASS);
connection.Encoding = Encoding.GetEncoding("windows-1250");