add user C# API

hi guys . .
i wanna add user in usermanager by API . .
i run this code :

static void Main()
    {
        MK mikrotik = new MK();
        if (mikrotik.Login("admin","password"))
        {
            mikrotik.Send("/tool/user-manager/user/add", false);
            mikrotik.Send("=customer=admin=", false);
            mikrotik.Send("=name=test=", false);
            mikrotik.Send("=password=testtest=", false);
            foreach (string h in mikrotik.Read())
            {
                Console.WriteLine(h);
            }
        }

but i face this error :

trap=message=no such command prefix !done

help me plz ..
what is wrong ?

mikrotik.Send(“=password=testtest=”, true);

last send must be “ture”

thanks for your help pal . .
but the problem not solved .. :frowning:
again i face this error :

!trap=message=no such command prefix !done

ok did you set ip ?

try this


MK mikrotik = new MK(“your ip here”);
if (mikrotik.Login(“admin”,“password”))
{
mikrotik.Send(“/tool/user-manager/user/add”);
mikrotik.Send(“=customer=admin”);
mikrotik.Send(“=username=test”);
mikrotik.Send(“=password=testtest”, true);
string result = mikrotik.Read();
}

or send me your clas and i will edit it

or if your mikrotik virson up 6.18 you must change " name " to " username"

mikrotik.Send(“=name=test=”, false);
to
mikrotik.Send(“=username=test”, false);

thank you so much bro :slight_smile:
it works . .
:slight_smile: :slight_smile: :slight_smile:

Focus on changes in virsons
like name, username
and in actvate profile you must change user to numbers
and if you want to use username in actvate you must use like this code
in new virson like 6.33 and up
this.mikrotik.Send(“/tool/user-manager/user/create-and-activate-profile”);
this.mikrotik.Send(“=customer=admin”);
this.mikrotik.Send(“=numbers=test”);
this.mikrotik.Send(“=profile=profilename”, true);and in virson like 5.25
this.mikrotik.Send(“/tool/user-manager/user/create-and-activate-profile”);
this.mikrotik.Send(“=customer=admin”);
this.mikrotik.Send(“=user=test”);
this.mikrotik.Send(“=profile=profilename”, true);

hi pal . .
i have another problem //
i use this code to find specific user :

            Console.Write("\n\n\t\t Write Your Username : ");
            string name = Console.ReadLine();
            mikrotik.Send("/tool/user-manager/user/print");
            mikrotik.Send("?username="+name, true);
            foreach (string h in mikrotik.Read())
            {
                Console.WriteLine( h);
            }

it works . .
but i want to display some part of info . .
for ex i want to display just username not all the information of that user . .
what should i do ?
how should i filter “h” to display specific info ?

you must define what info you want
like this
mikrotik.Send(“/tool/user-manager/user/print”);
mikrotik.Send(“?username=”+name);
mikrotik.Send(“=.proplist=.id,username,password,actual-profile”, true);

hey bro , you are my hero :slight_smile:))))
i have another question . .

    Stream connection;
    TcpClient con;

    public MK(string ip)
    {
        con = new TcpClient();
        con.Connect(ip, 8728);
        connection = (Stream)con.GetStream();
    }

with above code :
when i enter wrong ip my program goes crashing . .
i wanna when i enter wrong ip it show message that ip is wrong . .
what should i do with this code ?

i think the message for all wrong is one
for wrong ip , or port, or username and password ( one message for wrong in connection )
so you just use one message for any problem in connect.
i think it is no important for this

Hello bro…, I have a problem when registering the user, the problem is that I run the code twice, it only registers the user’s name and the password does not register what I can do to correct that error

MK mikrotik = new MK(“10.10.0.254”);
if (!mikrotik.Login(“itamar”, “12345678”))
{
Console.WriteLine(“No se pudo entrar a la cuenta”);
Console.ReadKey();
mikrotik.Close();
return;
}
mikrotik.Send(“/ip/hotspot/user/remove”,false);
mikrotik.Send(“=name=n”, false);
mikrotik.Send(“=password=n”, true);
mikrotik.Send(“=profile=profile1=”, true);


Console.WriteLine(“usuario creado”);

foreach (string h in mikrotik.Read())
{
Console.WriteLine(h);
}
Console.ReadKey();

Hello sister, I have a problem when registering the user, the problem is that I run the code twice, it only registers the user’s name and the password does not register what I can do to correct that error

MK mikrotik = new MK(“10.10.0.254”);
if (!mikrotik.Login(“itamar”, “12345678”))
{
Console.WriteLine(“No se pudo entrar a la cuenta”);
Console.ReadKey();
mikrotik.Close();
return;
}
mikrotik.Send(“/ip/hotspot/user/remove”,false);
mikrotik.Send(“=name=n”, false);
mikrotik.Send(“=password=n”, true);
mikrotik.Send(“=profile=profile1=”, true);


Console.WriteLine(“usuario creado”);

foreach (string h in mikrotik.Read())
{
Console.WriteLine(h);
}
Console.ReadKey();

Please note that there is a fault in the send method of the MT C# API example:

public void Send(string co, bool endsentence)
{
  byte[] bajty = Encoding.ASCII.GetBytes(co.ToCharArray());
  byte[] velikost = EncodeLength(bajty.Length);
  connection.Write(velikost, 0, velikost.Length);
  connection.Write(bajty, 0, bajty.Length);
  connection.WriteByte(0);
}

The problem is: the Send(string, bool) overload disregards the endsentence parameter, always sending a null byte terminator.

This should work (changing /remove into /add as it seems you’re trying to add a user):

mikrotik.Send("/ip/hotspot/user/add");
mikrotik.Send("=name=n");
mikrotik.Send("=password=n");
mikrotik.Send("=profile=profile1", true);

Edit: Wiki has been fixed