danikf / tik4net

Manage mikrotik routers with .NET C# code via ADO.NET like API or enjoy O/R mapper like highlevel api.
Apache License 2.0
177 stars 94 forks source link

Get id of any object #95

Open Shepard199 opened 10 months ago

Shepard199 commented 10 months ago

Hi, help me, please.For example, when I try to get the ID of any interface, I get an obscure id - (I get id - *83F, but the real id is 1198). Or how should I properly delete the interface I need?

Deantwo commented 8 months ago

The intern IDs in RouterOS are all *83F or similar. You can see the same if you do :put [/interface find] on the router's terminal.

Example code:

TikConnection con = ConnectionFactory.OpenConnection(Type.Api, _host, _username, _password);

ITikCommand cmd = conn.CreateCommand("/interface/print", conn.CreateParameter("name", _ifName));
string id = cmd.ExecuteScalar(".id");
System.Diagnostics.Debug.WriteLine($"Interface ({_ifName}) ID: {id}");

cmd = conn.CreateCommand("/interface/remove", conn.CreateParameter(".id", id));
cmd.ExecuteNonQuery();
System.Diagnostics.Debug.WriteLine($"Interface ({_ifName}) deleted");

But since interfaces are objects with identifier names, you can delete them directly without getting the ID, like this:

TikConnection con = ConnectionFactory.OpenConnection(Type.Api, _host, _username, _password);

cmd = conn.CreateCommand("/interface/remove", conn.CreateParameter(".id", _ifName));
cmd.ExecuteNonQuery();
System.Diagnostics.Debug.WriteLine($"Interface ({_ifName}) deleted");