using Api.Framework; using Api.Framework.Enums; using Api.Framework.SDK; using Api.Framework.Tools; using Chat.Framework.WXSdk; using Chat.Framework.WXSdk.Implement; using CsharpHttpHelper; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Weixin.CircleTools; using Weixin.CircleTools.Data; using Weixin.CircleTools.Entitys; using static CircleFriendsTools.Enums; namespace CircleFriendsTools { /// /// 评语 /// public class Comment { public long CreateTime { get; set; } public string Content { get; set; } } /// /// 监听朋友圈列表转发内容 /// class MonitorThread : TimerTask { private string[] Day = new string[] { "周末", "周一", "周二", "周三", "周四", "周五", "周六" }; private long count; //private bool isSend = false;//是否进入发送状态,成没成功和这个没关系 private string cache_key = "cache_key"; private static TbApi tbApi = new TbApi(); private bool IsRun = false; public MonitorThread() { } internal Class1 plugin; private static readonly Object ObjLock = new Object(); /// /// 等待发送的朋友圈任务 /// private static BlockingCollection waitSendCircleInfoList = new BlockingCollection(); static MonitorThread() { var th = new Thread(() => { foreach (var circleInfo in waitSendCircleInfoList) { try { //内容 var objectDesc = circleInfo.CircleContent; //评论 var comments = circleInfo.Comments; var clients = Chat.Framework.ChatClient.WXClient.Values.ToList(); foreach (var _item in clients) { var item = _item; if (Class1.Config.transpond_send_usernames.Count != 0 && !Class1.Config.transpond_send_usernames.Contains(item.WeixinHao)) { continue; } try { if ((item.WeixinType == WeixinType.Grpc微信 || item.WeixinType == WeixinType.Hook微信) && item.Status == WxStatus.在线) { EventClient.OnEvent("", $"【朋友圈工具】 => {item.User.Nick}({item.WeixinHao}),准备开始发送朋友圈~"); List tbInfoTempGroups = ApiClient.GetSession().FindTbInfoTempGroups();//获取tb数据数据集合缓存 var infoTemp = tbInfoTempGroups.FirstOrDefault(f => f.name.Trim() == item.WeixinHao.Trim() && !f.onoff); if (infoTemp == null) return; if (Class1.Config.TransmitStyle == TransmitStyleType.仅转发原文) item.SendCircle(objectDesc);//发送朋友圈的主内容 else { var _comment = new List(); if (Class1.Config.AutoTurn) { #region var tb = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈 && f.username == infoTemp.adzone_pid_cps_name);//用户设置的推广位 if (tb == null) throw new Exception("阿里妈妈推广位异常,请检测后重试!"); #region 淘宝转链 if (tb != null) { var api = CpsClient.CreateAlimamaRequest(tb); foreach (var comment in comments) { var regs = Regex.Matches(comment.Content, "(?<口令符号_前缀>[^A-Za-z0-9]?(?<淘口令>[A-Za-z0-9]{11})(?<口令符号_后缀>)[^A-Za-z0-9]?)"); if (regs.Count > 0) { foreach (Match temp in regs) { try { if (temp.Success) { var first_fuhao = temp.Groups["口令符号_前缀"].Value;//口令符号 var tklInfo = tbApi.TKLToOwnTKL(temp.Groups["淘口令"].Value, infoTemp.adzone_pid, api);//获取新的淘口令信息 if (tklInfo != null) { //淘口令的前/后符号有没有设置.有设置将替换掉 var tkl = new TBHelper.TbAnalysis().ReplaceTklModifier(tklInfo.tkl); if (!string.IsNullOrWhiteSpace(Class1.Config.AppendComment)) _comment.Add(Class1.Config.AppendComment.Replace("[转链口令]", tkl)); else _comment.Add(tkl); //_comment.Add(comment.Content.Replace(first_fuhao, tkl)); break; } } } catch (Exception ex) { EventClient.OnEvent("", $"机器人:{item.WeixinHao},评语转换淘口令失败 - {comment.Content},{ex.Message},{ex.StackTrace}"); } } } else _comment.Add(comment.Content); } } #endregion #endregion } else _comment = comments.Select(f => f.Content).ToList(); var json = item.SendCircle(objectDesc);//发送朋友圈的主内容 if (string.IsNullOrWhiteSpace(json)) EventClient.OnEvent("", $"【朋友圈工具】 => {item.WeixinHao},转发失败"); else EventClient.OnEvent("", $"【朋友圈工具】 => {item.WeixinHao},转发成功"); var reg = Regex.Match(json.Replace("", ""), @"(?\d{15,})");//获取返回的朋友圈id if (reg.Success) { var id = reg.Groups["id"].Value; //EventClient.OnEvent(this, $"【朋友圈工具】 => {item.WeixinHao}_{id}"); var commentStr = string.Join("[分段]", _comment); if (!string.IsNullOrWhiteSpace(commentStr)) { EventClient.OnEvent("", $"【朋友圈工具】 => {item.WeixinHao},{commentStr}"); item.SendCircleComment(item.User.Username, id, commentStr); } } else { //EventClient.OnEvent(this, $"【朋友圈工具】 => {item.WeixinHao},{json}"); } } } } catch (Exception ex) { EventClient.OnEvent("", $"朋友圈工具 - 朋友圈检测 - 转发:{item.User.Nick}({item.WeixinHao}){ex.Message},{ex.StackTrace}"); } } //}); EventClient.OnEvent("", "【朋友圈工具】 => 转发完毕"); } catch (Exception e) { EventClient.OnEvent("", $"【朋友圈工具】 => 转发异常:{e.Message}"); } } }); th.IsBackground = true; th.Start(); } // static List weixinhaos = new List() { "liu454097", "zhengbaolingshipu520", "Cmeiliy", "WY1164314651", "L13291035216", "z15982388678" }; public override async void Run(object state, bool timedOut) { try { count++; //朋友圈检测 if (!string.IsNullOrWhiteSpace(Class1.Config.MonitorCrowd) && ((state != null && state.ToString() == "立即检测") || count % (Class1.Config.MonitorIntervalTime * 60) == 0)) { EventClient.OnEvent(this, $"【朋友圈工具】 => 定时执行朋友圈跟发,状态:{(IsRun ? "历史任务还在执行中" : "历史任务未执行")}"); if (!IsRun) { IsRun = true; var clients = GetRandomList(Chat.Framework.ChatClient.WXClient.Values.ToList()); //获取机器人集合 try { string objectDesc = string.Empty; //要转发的朋友圈内容 List comments = new List(); //评论内容 var isNews = false; //判断是否有新的动态 #region 这里主要是检索所有在线的机器人列表中,监听用户的朋友圈内容以及评论数据 lock (ObjLock) { string newestTime = string.Empty; int ii = 1; foreach (var item in clients) { try { if ((item.WeixinType == WeixinType.Grpc微信 || item.WeixinType == WeixinType.Hook微信) && item.Status == WxStatus.在线) { //if (!weixinhaos.Contains(item.WeixinHao)) continue; var monitorCrowd = Class1.Config.MonitorCrowd.Trim(); //undone 获取用户真正的微信id var news = item.FindCircle(monitorCrowd); //获取朋友圈的动态 if (news != null && news.Count == 0 && (state != null && state.ToString() == "立即检测")) EventClient.OnEvent(this, $"【朋友圈工具】 => 朋友圈检测({item.WeixinHao}):{Class1.Config.MonitorInfoHistoryTime} - {news.Count}"); if (news != null && news.Count != 0) { if (news[0].ContainsKey("CreateTime")) newestTime = news[0]["CreateTime"].ToString(); //监听的微信id最新动态发送时间 //StringBuilder strb = new StringBuilder(); //if ((state != null && state.ToString() == "立即检测")) //{ // strb.AppendLine($"{item.WeixinHao} - {Class1.Config.MonitorInfoHistoryTime} - {newestTime}"); // if (news[0].ContainsKey("ObjectDesc")) // objectDesc = news[0]["ObjectDesc"].ToString(); //新动态的内容部分 // strb.AppendLine($"objectDesc = {objectDesc}"); // ArrayList _commentUserList = null; // if (news[0].ContainsKey("CommentUserList")) // _commentUserList = news[0]["CommentUserList"] as ArrayList; //评论用户列表 // strb.AppendLine(); // strb.AppendLine($"commentUserList = {_commentUserList}"); // ApiClient.SendNewDingdingMessage(strb.ToString(), "https://oapi.dingtalk.com/robot/send?access_token=3d1e19c2965752d07ade14cf99abb940d6bd0ddcf1b7aaebd831b95d339a0a78"); //} if (!string.IsNullOrEmpty(Class1.Config.MonitorInfoHistoryTime) && long.Parse(Class1.Config.MonitorInfoHistoryTime) >= long.Parse(newestTime)) //没有最新的动态 { if ((state != null && state.ToString() == "立即检测")) EventClient.OnEvent(this, $"【朋友圈工具】 => 朋友圈检测({item.WeixinHao}):没有新动态({newestTime})"); if (ii > 3) break; else { Thread.Sleep(500); ii++; continue; } } if (news[0].ContainsKey("ObjectDesc")) objectDesc = news[0]["ObjectDesc"].ToString(); //新动态的内容部分 var reg = Regex.Match(objectDesc, @"(?<地址信息>\)"); if (reg.Success) { var location = reg.Groups["地址信息"].Value; var locationContent = string.Empty; if (!string.IsNullOrWhiteSpace(Class1.Config.LocationContent)) { // //Regex.Replace(“abc”,@“(?.*?)”,"[图片]${str}"); locationContent = @""""; } objectDesc = objectDesc.Replace(location, locationContent); } if (Class1.Config.TransmitStyle == TransmitStyleType.仅转发原文) { isNews = true; waitSendCircleInfoList.TryAdd(new WaitSendCircleInfo() { CircleContent = objectDesc, Comments = comments }); break; //设置为仅转发原文 } ArrayList commentUserList = null; if (news[0].ContainsKey("CommentUserList")) commentUserList = news[0]["CommentUserList"] as ArrayList; //评论用户列表 else { if (Class1.Config.TransmitStyle == TransmitStyleType.原文及评论) { isNews = false; continue; } } if (commentUserList != null && commentUserList.Count != 0) { for (int i = 0; i < commentUserList.Count; i++) { try { if (commentUserList[i] is Dictionary user && user.Count != 0) { var username = string.Empty; if (user.ContainsKey("Username")) username = user["Username"].ToString(); if (monitorCrowd == username || Class1.Config.MonitorCrowd == username)//发送者给自己评论时. { if (user.ContainsKey("Content")) comments.Add(new Comment { Content = user["Content"].ToString(), CreateTime = long.Parse(user["CreateTime"].ToString()) }); } } } catch (Exception ex) { throw ex; } } //评论不为空 if (comments.Count != 0) { comments.Sort((c1, c2) => c1.CreateTime.CompareTo(c2.CreateTime)); isNews = true; waitSendCircleInfoList.TryAdd(new WaitSendCircleInfo() { CircleContent = objectDesc, Comments = comments }); break; } } if (Class1.Config.Turn_Obstinate && !string.IsNullOrEmpty(objectDesc)) { isNews = true; waitSendCircleInfoList.TryAdd(new WaitSendCircleInfo() { CircleContent = objectDesc, Comments = comments }); break; } } } } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - 朋友圈检测 - 采集:{ex.Message} - {ex.StackTrace}"); } } if (isNews) { if (!string.IsNullOrWhiteSpace(newestTime)) { Class1.Config.MonitorInfoHistoryTime = newestTime; Util.Save(Class1.Config); //更新记录时间 } } } #endregion #region 这里主要是让在线的机器人都转发上面获取到的最新动态 if (isNews) { return; foreach (var _item in clients) { var item = _item; if (Class1.Config.transpond_send_usernames.Count != 0 && !Class1.Config.transpond_send_usernames.Contains(item.WeixinHao)) continue; await Task.Run(() => { try { if ((item.WeixinType == WeixinType.Grpc微信 || item.WeixinType == WeixinType.Hook微信) && item.Status == WxStatus.在线) { EventClient.OnEvent("", $"【朋友圈工具】 => {item.User.Nick}({item.WeixinHao}),准备开始发送朋友圈~"); List tbInfoTempGroups = ApiClient.GetSession().FindTbInfoTempGroups();//获取tb数据数据集合缓存 var infoTemp = tbInfoTempGroups.FirstOrDefault(f => f.name.Trim() == item.WeixinHao.Trim() && !f.onoff); if (infoTemp == null) return; if (Class1.Config.TransmitStyle == TransmitStyleType.仅转发原文) item.SendCircle(objectDesc);//发送朋友圈的主内容 else { var _comment = new List(); if (Class1.Config.AutoTurn) { #region var tb = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈 && f.username == infoTemp.adzone_pid_cps_name);//用户设置的推广位 if (tb == null) throw new Exception("阿里妈妈推广位异常,请检测后重试!"); #region 淘宝转链 if (tb != null) { var api = CpsClient.CreateAlimamaRequest(tb); foreach (var comment in comments) { var regs = Regex.Matches(comment.Content, "(?<口令符号_前缀>[^A-Za-z0-9]?(?<淘口令>[A-Za-z0-9]{11})(?<口令符号_后缀>)[^A-Za-z0-9]?)"); if (regs.Count > 0) { foreach (Match temp in regs) { try { if (temp.Success) { var first_fuhao = temp.Groups["口令符号_前缀"].Value;//口令符号 var tklInfo = tbApi.TKLToOwnTKL(temp.Groups["淘口令"].Value, infoTemp.adzone_pid, api);//获取新的淘口令信息 if (tklInfo != null) { //淘口令的前/后符号有没有设置.有设置将替换掉 var tkl = new TBHelper.TbAnalysis().ReplaceTklModifier(tklInfo.tkl); if (!string.IsNullOrWhiteSpace(Class1.Config.AppendComment)) _comment.Add(Class1.Config.AppendComment.Replace("[转链口令]", tkl)); else _comment.Add(tkl); //_comment.Add(comment.Content.Replace(first_fuhao, tkl)); break; } } } catch (Exception ex) { EventClient.OnEvent(this, $"机器人:{item.WeixinHao},评语转换淘口令失败 - {comment.Content},{ex.Message},{ex.StackTrace}"); } } } else _comment.Add(comment.Content); } } #endregion #endregion } else _comment = comments.Select(f => f.Content).ToList(); var json = item.SendCircle(objectDesc);//发送朋友圈的主内容 if (string.IsNullOrWhiteSpace(json)) EventClient.OnEvent(this, $"【朋友圈工具】 => {item.WeixinHao},转发失败"); else EventClient.OnEvent(this, $"【朋友圈工具】 => {item.WeixinHao},转发成功"); var reg = Regex.Match(json.Replace("", ""), @"(?\d{15,})");//获取返回的朋友圈id if (reg.Success) { var id = reg.Groups["id"].Value; //EventClient.OnEvent(this, $"【朋友圈工具】 => {item.WeixinHao}_{id}"); var commentStr = string.Join("[分段]", _comment); if (!string.IsNullOrWhiteSpace(commentStr)) { EventClient.OnEvent(this, $"【朋友圈工具】 => {item.WeixinHao},{commentStr}"); item.SendCircleComment(item.User.Username, id, commentStr); } } else { //EventClient.OnEvent(this, $"【朋友圈工具】 => {item.WeixinHao},{json}"); } } } } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - 朋友圈检测 - 转发:{item.User.Nick}({item.WeixinHao}){ex.Message},{ex.StackTrace}"); } }); } //}); EventClient.OnEvent(this, $"【朋友圈工具】 => 转发完毕"); } #endregion } catch (Exception) { } finally { IsRun = false; } } } #region web接口检测 if (count % (Class1.Config.CustomWebApiIntervalTime * 60) == 0) { if (!string.IsNullOrWhiteSpace(Class1.Config.CustomWebApi)) { try { var http = new HttpHelper(); var text = http.GetHtml(HttpExtend.GetItem(http, Class1.Config.CustomWebApi.Replace(".txt", ".php"))).Html; if (!string.IsNullOrWhiteSpace(text)) { var text_MD5 = Api.Framework.Tools.Util.GetMd5_32byte(text); if (Class1.Config.CustomWebApiTextMD5 != text_MD5) { Class1.Config.CustomWebApiTextMD5 = text_MD5; Util.Save(Class1.Config); var clients = Chat.Framework.ChatClient.WXClient.Values.ToList(); var values = text.Split(new string[] { "[评语]" }, StringSplitOptions.RemoveEmptyEntries); if (values.Length != 0) { var decided_tkl = string.Empty; foreach (WeixinBase item in clients) { try { if ((item.WeixinType == WeixinType.Grpc微信 || item.WeixinType == WeixinType.Hook微信) && item.Status == WxStatus.在线) { var jsonObject = item.SendCircle(values[0]); if (jsonObject == null) throw new Exception("请检测是否包含图片地址"); if (values.Length == 2) { var reg = Regex.Match(jsonObject.Replace("", ""), @"(?\d{15,})");//获取返回的朋友圈id if (reg.Success) { var id = reg.Groups["id"].Value; if (!string.IsNullOrWhiteSpace(values[1])) { var session = ApiClient.GetSession(); var tbInfoTempGroups = session.FindTbInfoTempGroups();//获取tb数据数据集合缓存 var infoTemp = tbInfoTempGroups.FirstOrDefault(f => f.name.Trim() == item.WeixinHao.Trim() && !f.onoff); var tb = CpsClient.Members.FirstOrDefault(f => f.cpstype == CpsType.阿里妈妈 && f.username == infoTemp.adzone_pid_cps_name);//用户设置的推广位 if (tb == null) throw new Exception("阿里妈妈推广位异常,请检测后重试!"); #region 淘宝转链 if (tb != null) { var finally_comment = values[1]; ; if (Class1.Config.AutoTurn) { var api = CpsClient.CreateAlimamaRequest(tb); var tempstr = string.IsNullOrWhiteSpace(decided_tkl) ? values[1] : decided_tkl; var regs = Regex.Matches(tempstr, "(?<口令符号_前缀>[^A-Za-z0-9]?(?<淘口令>[A-Za-z0-9]{11})(?<口令符号_后缀>)[^A-Za-z0-9]?)"); if (regs.Count > 0) { foreach (Match temp in regs) { try { if (temp.Success) { var first_fuhao = temp.Groups["口令符号_前缀"].Value;//口令符号 var suspicious_tkl = temp.Groups["淘口令"].Value; var tklInfo = tbApi.TKLToOwnTKL(suspicious_tkl, infoTemp.adzone_pid, api);//获取新的淘口令信息 if (tklInfo != null) { decided_tkl = first_fuhao; //淘口令的前/后符号有没有设置.有设置将替换掉 var tkl = new TBHelper.TbAnalysis().ReplaceTklModifier(tklInfo.tkl); finally_comment = values[1].Replace(first_fuhao, tkl); break; } } } catch (Exception ex) { EventClient.OnEvent(this, $"机器人:{item.WeixinHao},评语转换淘口令失败 - {values[1]},{ex.Message},{ex.StackTrace}"); } } } } item.SendCircleComment(item.User.Username, id, finally_comment); #endregion } } } } } } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - web接口:({item.WeixinHao}) => {ex.Message}"); } } } } } } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - web接口:{ex.Message}"); } } } #endregion #region 自动点赞 if (Class1.Config.AutoPraise && count % (Class1.Config.AutoPraiseInterval * 60) == 0) { var robots = Chat.Framework.ChatClient.WXClient.Where(item => (item.Value.WeixinType == WeixinType.Grpc微信 || item.Value.WeixinType == WeixinType.Hook微信) && item.Value.Status == WxStatus.在线); Parallel.ForEach(robots, item => { var robot = item.Value; try { var circles = robot.FindCircles(); foreach (var temp in circles) { try { var likeUserList = temp["LikeUserList"] as ArrayList; if (likeUserList != null && likeUserList.Count != 0) { var query = from Dictionary likeUser in likeUserList where likeUser["Username"].ToString() == robot.User.Username select likeUser; if (query != null) robot.SendCirclePraise(temp["Username"].ToString(), temp["Id"].ToString()); } else robot.SendCirclePraise(temp["Username"].ToString(), temp["Id"].ToString()); Thread.Sleep(1521); } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - 自动点赞:{ex.Message}"); } } } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - 自动点赞:{ex.Message}"); } }); } #endregion #region 定时发送任务 if (count % 60 == 0) { var session = ApiClient.GetSession(); var pddinfo_temp_groups = session.FindTaskInfosTempGroups(); //读取缓存中的数据 if (pddinfo_temp_groups != null && pddinfo_temp_groups.Count != 0) { string week = Day[Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"))].ToString(); //获取到今天是星期几 var isSend = false;//还原 foreach (var item in pddinfo_temp_groups) { try { if (item.task_working_day.Contains(week)) { var result = ApiClient.Cache.Get>(cache_key); if (result != null && result.Contains(item.id)) continue; var task_time = DateTime.Parse(item.task_time); var now = DateTime.Now; var dValue = (now - task_time).TotalMinutes; if (0 < dValue && dValue < 5) { if (result == null) result = new List(); result.Add(item.id); ApiClient.Cache.Set(cache_key, result, 5); var clients = Chat.Framework.ChatClient.WXClient.Values.ToList().Where(f => (f.WeixinType == WeixinType.Grpc微信 || f.WeixinType == WeixinType.Hook微信) && f.Status == WxStatus.在线); var robotnames = item.robotnames.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(f => f.Trim()).ToList(); foreach (var client in clients) { try { if (!robotnames.Contains(client.WeixinHao.Trim())) continue; isSend = true; EventClient.OnEvent(this, "定时任务:" + client.WeixinHao + ":准备发了"); var json = client.SendCircle(item.task_sendtext);//发送朋友圈的主内容 if (string.IsNullOrWhiteSpace(json)) EventClient.OnEvent(this, "定时任务:" + client.WeixinHao + ":发送失败"); else EventClient.OnEvent(this, "定时任务:" + client.WeixinHao + ":已经发了"); var reg = Regex.Match(json.Replace("", ""), @"(?\d{15,})");//获取返回的朋友圈id if (reg.Success) { var id = reg.Groups["id"].Value; if (!string.IsNullOrWhiteSpace(item.task_comment)) client.SendCircleComment(client.User.Username, id, item.task_comment); //替换淘口令 } } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - 定时发送任务:{ex.Message},{ex.StackTrace}"); } } if (isSend && item.is_circulate == Enums.ChooseType.否)//不是循环发送的任务,将本次发送的工作日给删除了. { var workingDays = item.task_working_day.Replace(" ", "").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList(); workingDays.Remove(week); if (workingDays.Count == 0)//任务中的所有工作日都已经执行完成,删除该任务 session.Deleteable(item).ExecuteCommand(); else//任务中的工作还存在,将本次工作日删除后进行更新 { var task_working_day = string.Join(",", workingDays); item.task_working_day = task_working_day; //session.Updateable(item).ExecuteCommand(); session.ExcuteSQL("update fl_plugin_circletools_taskinfos set task_working_day = @task_working_day where id = @id", new { task_working_day = task_working_day, id = item.id }); } session.FindTaskInfosTempGroups(true); } } } } catch (Exception ex) { EventClient.OnEvent(this, $"朋友圈工具 - 定时发送任务:{ex.Message}"); } } } } #endregion } catch (Exception ex) { SessionExt.Clear(); EventClient.OnEvent(this, $"朋友圈工具:{ex.Message}\r\n{ex.StackTrace}"); } } private static List GetRandomList(List inputList) { try { //Copy to a array T[] copyArray = new T[inputList.Count]; inputList.CopyTo(copyArray); //Add range List copyList = new List(); copyList.AddRange(copyArray); //Set outputList and random List outputList = new List(); Random rd = new Random(DateTime.Now.Millisecond); while (copyList.Count > 0) { //Select an index and item int rdIndex = rd.Next(0, copyList.Count - 1); T remove = copyList[rdIndex]; //remove it from copyList and add it to output copyList.Remove(remove); outputList.Add(remove); } return outputList; } catch (Exception ex) { } return inputList; } } }