Exemplo n.º 1
0
        /// <summary>
        /// 对用户操作命令进行操作
        /// </summary>
        /// <param name="list">参数列表</param>
        public static void UserAction(string[] list)
        {
            if (list.Length <= 1)
            {
                int count = 0;
                Console.WriteLine("当前在线用户: " + (count = User.UserNowCount()));
                Console.WriteLine("所有用户数量: " + User.UserListCount());
                if (count > 0)
                {
                    Console.WriteLine("在线用户列表:");
                    foreach (string u in User.UserNowList())
                    {
                        Console.WriteLine(" " + u);
                    }
                }
            }
            else
            {
                switch (list[1].Trim().ToLower())
                {
                case "a":
                case "add":
                    string name, password, ip;
                    name     = ReadNoEmptyValue("用户名:");
                    password = ReadNoEmptyValue("密码:");
                    ip       = ReadNoEmptyValue("IP地址规则,支持正则(*将做完全匹配替换):");
                    if (User.AddUserToList(name, password, ip))
                    {
                        Console.WriteLine("添加用户成功!");
                    }
                    else
                    {
                        Console.WriteLine("添加用户失败,或许用户已存在!");
                    }
                    break;

                case "d":
                case "delete":
                    if (list.Length < 3)
                    {
                        Console.WriteLine("请尝试命令: user delete [name] | user deleteAll");
                    }
                    else
                    {
                        if (User.DeleteUser(list[2].Trim()))
                        {
                            Console.WriteLine("删除成功!");
                        }
                        else
                        {
                            Console.WriteLine("删除 " + list[2].Trim() + " 出错!");
                        }
                    }
                    break;

                case "off":
                case "offline":
                    if (list.Length < 3)
                    {
                        Console.WriteLine("尝试: user offline [name]");
                    }
                    else
                    {
                        if (User.OfflineUser(list[2].Trim()))
                        {
                            Console.WriteLine("下线成功!");
                        }
                        else
                        {
                            Console.WriteLine("下线 " + list[2].Trim() + " 出错!");
                        }
                    }
                    break;

                case "da":
                case "deleteall":
                    User.DeleteAllUser();
                    if (User.UserListCount() == 0)
                    {
                        Console.WriteLine("用户已清空!");
                    }
                    else
                    {
                        Console.WriteLine("用户清空出错!");
                    }
                    break;

                case "s":
                case "save":
                    if (Configure.WriteUserList(User.GetUserListTable()))
                    {
                        Console.WriteLine("成功保存用户列表!");
                    }
                    else
                    {
                        Console.WriteLine("保存出现异常");
                    }
                    break;

                case "l":
                case "list":
                    string[] l = User.UserNowList();
                    if (l.Length > 0)
                    {
                        Console.WriteLine("在线用户列表:");
                        foreach (string u in l)
                        {
                            Console.WriteLine(" " + u);
                        }
                    }
                    else
                    {
                        Console.WriteLine("当前没有用户在线.");
                    }
                    l = User.UserList();
                    if (l.Length > 0)
                    {
                        Console.WriteLine("\n所有用户列表:");
                        foreach (string u in l)
                        {
                            Console.WriteLine(" " + u);
                        }
                    }
                    else
                    {
                        Console.WriteLine("\n系统没有任何用户.");
                    }
                    break;

                case "h":
                case "?":
                case "/?":
                case "\\?":
                case "--help":
                case "help":
                    help();
                    break;

                default:
                    Console.WriteLine("Usage: user [ add | delete [name] | offline [user] | save | deleteAll | list | help]");
                    break;
                }
            }
        }